Skip to content

Commit 6206086

Browse files
committed
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`
2 parents 59c0f29 + 3d1f83e commit 6206086

File tree

5 files changed

+15
-4
lines changed

5 files changed

+15
-4
lines changed

clippy_lints/src/methods/map_identity.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub(super) fn check(
1313
expr: &hir::Expr<'_>,
1414
caller: &hir::Expr<'_>,
1515
map_arg: &hir::Expr<'_>,
16+
name: &str,
1617
_map_span: Span,
1718
) {
1819
let caller_ty = cx.typeck_results().expr_ty(caller);
@@ -29,7 +30,7 @@ pub(super) fn check(
2930
MAP_IDENTITY,
3031
sugg_span,
3132
"unnecessary map of the identity function",
32-
"remove the call to `map`",
33+
&format!("remove the call to `{}`", name),
3334
String::new(),
3435
Applicability::MachineApplicable,
3536
)

clippy_lints/src/methods/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2472,7 +2472,7 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio
24722472
}
24732473
}
24742474
},
2475-
("map", [m_arg]) => {
2475+
(name @ ("map" | "map_err"), [m_arg]) => {
24762476
if let Some((name, [recv2, args @ ..], span2)) = method_call(recv) {
24772477
match (name, args) {
24782478
("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
24842484
_ => {},
24852485
}
24862486
}
2487-
map_identity::check(cx, expr, recv, m_arg, span);
2487+
map_identity::check(cx, expr, recv, m_arg, name, span);
24882488
},
24892489
("map_or", [def, map]) => option_map_or_none::check(cx, expr, recv, def, map),
24902490
(name @ "next", args @ []) => {

tests/ui/map_identity.fixed

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ fn main() {
1616
let _: Result<i8, f32> = Err(2.3).map(|x: i8| {
1717
return x + 3;
1818
});
19+
let _: Result<u32, u32> = Ok(1);
20+
let _: Result<u32, u32> = Ok(1).map_err(|a: u32| a * 42);
1921
}
2022

2123
fn not_identity(x: &u16) -> u16 {

tests/ui/map_identity.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ fn main() {
1818
let _: Result<i8, f32> = Err(2.3).map(|x: i8| {
1919
return x + 3;
2020
});
21+
let _: Result<u32, u32> = Ok(1).map_err(|a| a);
22+
let _: Result<u32, u32> = Ok(1).map_err(|a: u32| a * 42);
2123
}
2224

2325
fn not_identity(x: &u16) -> u16 {

tests/ui/map_identity.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,11 @@ LL | | return x;
3333
LL | | });
3434
| |______^ help: remove the call to `map`
3535

36-
error: aborting due to 5 previous errors
36+
error: unnecessary map of the identity function
37+
--> $DIR/map_identity.rs:21:36
38+
|
39+
LL | let _: Result<u32, u32> = Ok(1).map_err(|a| a);
40+
| ^^^^^^^^^^^^^^^ help: remove the call to `map_err`
41+
42+
error: aborting due to 6 previous errors
3743

0 commit comments

Comments
 (0)