Auto merge of #8487 - dswij:8478, r=giraffate

[`map_identity`] checks for needless `map_err`

Closes #8478

changelog: [`map_identity`] checks for needless `map_err`
This commit is contained in:
bors 2022-03-28 00:25:45 +00:00
commit 6206086dd5
5 changed files with 15 additions and 4 deletions

View file

@ -13,6 +13,7 @@ pub(super) fn check(
expr: &hir::Expr<'_>, expr: &hir::Expr<'_>,
caller: &hir::Expr<'_>, caller: &hir::Expr<'_>,
map_arg: &hir::Expr<'_>, map_arg: &hir::Expr<'_>,
name: &str,
_map_span: Span, _map_span: Span,
) { ) {
let caller_ty = cx.typeck_results().expr_ty(caller); let caller_ty = cx.typeck_results().expr_ty(caller);
@ -29,7 +30,7 @@ pub(super) fn check(
MAP_IDENTITY, MAP_IDENTITY,
sugg_span, sugg_span,
"unnecessary map of the identity function", "unnecessary map of the identity function",
"remove the call to `map`", &format!("remove the call to `{}`", name),
String::new(), String::new(),
Applicability::MachineApplicable, Applicability::MachineApplicable,
) )

View file

@ -2472,7 +2472,7 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio
} }
} }
}, },
("map", [m_arg]) => { (name @ ("map" | "map_err"), [m_arg]) => {
if let Some((name, [recv2, args @ ..], span2)) = method_call(recv) { if let Some((name, [recv2, args @ ..], span2)) = method_call(recv) {
match (name, args) { match (name, args) {
("as_mut", []) => option_as_ref_deref::check(cx, expr, recv2, m_arg, true, msrv), ("as_mut", []) => option_as_ref_deref::check(cx, expr, recv2, m_arg, true, msrv),
@ -2484,7 +2484,7 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio
_ => {}, _ => {},
} }
} }
map_identity::check(cx, expr, recv, m_arg, span); map_identity::check(cx, expr, recv, m_arg, name, span);
}, },
("map_or", [def, map]) => option_map_or_none::check(cx, expr, recv, def, map), ("map_or", [def, map]) => option_map_or_none::check(cx, expr, recv, def, map),
(name @ "next", args @ []) => { (name @ "next", args @ []) => {

View file

@ -16,6 +16,8 @@ fn main() {
let _: Result<i8, f32> = Err(2.3).map(|x: i8| { let _: Result<i8, f32> = Err(2.3).map(|x: i8| {
return x + 3; return x + 3;
}); });
let _: Result<u32, u32> = Ok(1);
let _: Result<u32, u32> = Ok(1).map_err(|a: u32| a * 42);
} }
fn not_identity(x: &u16) -> u16 { fn not_identity(x: &u16) -> u16 {

View file

@ -18,6 +18,8 @@ fn main() {
let _: Result<i8, f32> = Err(2.3).map(|x: i8| { let _: Result<i8, f32> = Err(2.3).map(|x: i8| {
return x + 3; return x + 3;
}); });
let _: Result<u32, u32> = Ok(1).map_err(|a| a);
let _: Result<u32, u32> = Ok(1).map_err(|a: u32| a * 42);
} }
fn not_identity(x: &u16) -> u16 { fn not_identity(x: &u16) -> u16 {

View file

@ -33,5 +33,11 @@ LL | | return x;
LL | | }); LL | | });
| |______^ help: remove the call to `map` | |______^ help: remove the call to `map`
error: aborting due to 5 previous errors error: unnecessary map of the identity function
--> $DIR/map_identity.rs:21:36
|
LL | let _: Result<u32, u32> = Ok(1).map_err(|a| a);
| ^^^^^^^^^^^^^^^ help: remove the call to `map_err`
error: aborting due to 6 previous errors