[map_identity]: recognize tuples

This commit is contained in:
y21 2023-06-13 15:21:14 +02:00
parent 2b030eb03d
commit d6fc606259
5 changed files with 96 additions and 10 deletions

View file

@ -22,6 +22,30 @@ fn main() {
let _ = Ok(1).map_err(std::convert::identity::<u32>);
}
fn issue7189() {
// should lint
let x = [(1, 2), (3, 4)];
let _ = x.iter();
let _ = x.iter();
let _ = x.iter();
let y = [(1, 2, (3, (4,))), (5, 6, (7, (8,)))];
let _ = y.iter();
// should not lint
let _ = x.iter().map(|(x, y)| (x, y, y));
let _ = x.iter().map(|(x, _y)| (x,));
let _ = x.iter().map(|(x, _)| (x,));
let _ = x.iter().map(|(x, ..)| (x,));
let _ = y.iter().map(|(x, y, (z, _))| (x, y, (z, z)));
let _ = y
.iter()
.map(|(x, y, (z, _)): &(i32, i32, (i32, (i32,)))| (x, y, (z, z)));
let _ = y
.iter()
.map(|(x, y, (z, (w,))): &(i32, i32, (i32, (i32,)))| (x, y, (z, (w,))));
}
fn not_identity(x: &u16) -> u16 {
*x
}

View file

@ -24,6 +24,32 @@ fn main() {
let _ = Ok(1).map_err(std::convert::identity::<u32>);
}
fn issue7189() {
// should lint
let x = [(1, 2), (3, 4)];
let _ = x.iter().map(|(x, y)| (x, y));
let _ = x.iter().map(|(x, y)| {
return (x, y);
});
let _ = x.iter().map(|(x, y)| return (x, y));
let y = [(1, 2, (3, (4,))), (5, 6, (7, (8,)))];
let _ = y.iter().map(|(x, y, (z, (w,)))| (x, y, (z, (w,))));
// should not lint
let _ = x.iter().map(|(x, y)| (x, y, y));
let _ = x.iter().map(|(x, _y)| (x,));
let _ = x.iter().map(|(x, _)| (x,));
let _ = x.iter().map(|(x, ..)| (x,));
let _ = y.iter().map(|(x, y, (z, _))| (x, y, (z, z)));
let _ = y
.iter()
.map(|(x, y, (z, _)): &(i32, i32, (i32, (i32,)))| (x, y, (z, z)));
let _ = y
.iter()
.map(|(x, y, (z, (w,))): &(i32, i32, (i32, (i32,)))| (x, y, (z, (w,))));
}
fn not_identity(x: &u16) -> u16 {
*x
}

View file

@ -40,5 +40,32 @@ error: unnecessary map of the identity function
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
error: unnecessary map of the identity function
--> $DIR/map_identity.rs:30:21
|
LL | let _ = x.iter().map(|(x, y)| (x, y));
| ^^^^^^^^^^^^^^^^^^^^^ help: remove the call to `map`
error: unnecessary map of the identity function
--> $DIR/map_identity.rs:31:21
|
LL | let _ = x.iter().map(|(x, y)| {
| _____________________^
LL | | return (x, y);
LL | | });
| |______^ help: remove the call to `map`
error: unnecessary map of the identity function
--> $DIR/map_identity.rs:34:21
|
LL | let _ = x.iter().map(|(x, y)| return (x, y));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove the call to `map`
error: unnecessary map of the identity function
--> $DIR/map_identity.rs:37:21
|
LL | let _ = y.iter().map(|(x, y, (z, (w,)))| (x, y, (z, (w,))));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove the call to `map`
error: aborting due to 10 previous errors