Skip to content

Commit

Permalink
Auto merge of #8487 - dswij:8478, r=giraffate
Browse files Browse the repository at this point in the history
[`map_identity`] checks for needless `map_err`

Closes #8478

changelog: [`map_identity`] checks for needless `map_err`
  • Loading branch information
bors committed Mar 28, 2022
2 parents 59c0f29 + 3d1f83e commit 6206086
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 4 deletions.
3 changes: 2 additions & 1 deletion clippy_lints/src/methods/map_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub(super) fn check(
expr: &hir::Expr<'_>,
caller: &hir::Expr<'_>,
map_arg: &hir::Expr<'_>,
name: &str,
_map_span: Span,
) {
let caller_ty = cx.typeck_results().expr_ty(caller);
Expand All @@ -29,7 +30,7 @@ pub(super) fn check(
MAP_IDENTITY,
sugg_span,
"unnecessary map of the identity function",
"remove the call to `map`",
&format!("remove the call to `{}`", name),
String::new(),
Applicability::MachineApplicable,
)
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
match (name, args) {
("as_mut", []) => option_as_ref_deref::check(cx, expr, recv2, m_arg, true, msrv),
Expand All @@ -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),
(name @ "next", args @ []) => {
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/map_identity.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ fn main() {
let _: Result<i8, f32> = Err(2.3).map(|x: i8| {
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 {
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/map_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ fn main() {
let _: Result<i8, f32> = Err(2.3).map(|x: i8| {
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 {
Expand Down
8 changes: 7 additions & 1 deletion tests/ui/map_identity.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,11 @@ LL | | return x;
LL | | });
| |______^ 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

0 comments on commit 6206086

Please sign in to comment.