Skip to content

Commit 3908154

Browse files
committed
same for tuple structs
1 parent 9bd7868 commit 3908154

File tree

4 files changed

+78
-11
lines changed

4 files changed

+78
-11
lines changed

clippy_utils/src/lib.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,6 +1913,9 @@ fn is_body_identity_function(cx: &LateContext<'_>, func: &Body<'_>) -> bool {
19131913
return false;
19141914
}
19151915

1916+
// NOTE: we're inside a (function) body, so this won't ICE
1917+
let qpath_res = |qpath, hir| cx.typeck_results().qpath_res(qpath, hir);
1918+
19161919
match (pat.kind, expr.kind) {
19171920
(PatKind::Binding(_, id, _, _), _) => {
19181921
path_to_local_id(expr, id) && cx.typeck_results().expr_adjustments(expr).is_empty()
@@ -1929,12 +1932,20 @@ fn is_body_identity_function(cx: &LateContext<'_>, func: &Body<'_>) -> bool {
19291932
.zip(arr)
19301933
.all(|(pat, expr)| check_pat(cx, pat, expr))
19311934
},
1935+
(PatKind::TupleStruct(pat_ident, field_pats, dotdot), ExprKind::Call(ident, fields))
1936+
if dotdot.as_opt_usize().is_none()
1937+
&& let ExprKind::Path(ident) = ident.kind =>
1938+
{
1939+
field_pats.len() == fields.len()
1940+
&& qpath_res(&pat_ident, pat.hir_id) == qpath_res(&ident, expr.hir_id)
1941+
&& (field_pats.iter())
1942+
.zip(fields)
1943+
.all(|(pat, expr)| check_pat(cx, pat, expr))
1944+
},
19321945
(
19331946
PatKind::Struct(pat_ident, field_pats, false),
19341947
ExprKind::Struct(ident, fields, hir::StructTailExpr::None),
19351948
) => {
1936-
// NOTE: we're inside a (function) body, so this won't ICE
1937-
let qpath_res = |qpath, hir| cx.typeck_results().qpath_res(qpath, hir);
19381949
field_pats.len() == fields.len()
19391950
&& qpath_res(&pat_ident, pat.hir_id) == qpath_res(ident, expr.hir_id)
19401951
&& field_pats.iter().all(|field_pat| {

tests/ui/f1.fixed

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,20 @@ mod foo {
77
pub foo: u8,
88
pub bar: u8,
99
}
10+
11+
#[derive(Clone, Copy)]
12+
pub struct Foo2(pub u8, pub u8);
1013
}
11-
use foo::Foo;
14+
use foo::{Foo, Foo2};
1215

1316
struct Bar {
1417
foo: u8,
1518
bar: u8,
1619
}
1720

18-
fn main() {
21+
struct Bar2(u8, u8);
22+
23+
fn structs() {
1924
let x = [Foo { foo: 0, bar: 0 }];
2025

2126
let _ = x.into_iter();
@@ -40,3 +45,20 @@ fn main() {
4045
// don't lint: switched field assignment
4146
let _ = x.into_iter().map(|Foo { foo, bar }| Foo { foo: bar, bar: foo });
4247
}
48+
49+
fn tuple_structs() {
50+
let x = [Foo2(0, 0)];
51+
52+
let _ = x.into_iter();
53+
//~^ map_identity
54+
55+
// still lint when different paths are used for the same struct
56+
let _ = x.into_iter();
57+
//~^ map_identity
58+
59+
// don't lint: same fields but different structs
60+
let _ = x.into_iter().map(|Foo2(foo, bar)| Bar2(foo, bar));
61+
62+
// don't lint: switched field assignment
63+
let _ = x.into_iter().map(|Foo2(foo, bar)| Foo2(bar, foo));
64+
}

tests/ui/f1.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,20 @@ mod foo {
77
pub foo: u8,
88
pub bar: u8,
99
}
10+
11+
#[derive(Clone, Copy)]
12+
pub struct Foo2(pub u8, pub u8);
1013
}
11-
use foo::Foo;
14+
use foo::{Foo, Foo2};
1215

1316
struct Bar {
1417
foo: u8,
1518
bar: u8,
1619
}
1720

18-
fn main() {
21+
struct Bar2(u8, u8);
22+
23+
fn structs() {
1924
let x = [Foo { foo: 0, bar: 0 }];
2025

2126
let _ = x.into_iter().map(|Foo { foo, bar }| Foo { foo, bar });
@@ -40,3 +45,20 @@ fn main() {
4045
// don't lint: switched field assignment
4146
let _ = x.into_iter().map(|Foo { foo, bar }| Foo { foo: bar, bar: foo });
4247
}
48+
49+
fn tuple_structs() {
50+
let x = [Foo2(0, 0)];
51+
52+
let _ = x.into_iter().map(|Foo2(foo, bar)| Foo2(foo, bar));
53+
//~^ map_identity
54+
55+
// still lint when different paths are used for the same struct
56+
let _ = x.into_iter().map(|Foo2(foo, bar)| foo::Foo2(foo, bar));
57+
//~^ map_identity
58+
59+
// don't lint: same fields but different structs
60+
let _ = x.into_iter().map(|Foo2(foo, bar)| Bar2(foo, bar));
61+
62+
// don't lint: switched field assignment
63+
let _ = x.into_iter().map(|Foo2(foo, bar)| Foo2(bar, foo));
64+
}

tests/ui/f1.stderr

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: unnecessary map of the identity function
2-
--> tests/ui/f1.rs:21:26
2+
--> tests/ui/f1.rs:26:26
33
|
44
LL | let _ = x.into_iter().map(|Foo { foo, bar }| Foo { foo, bar });
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove the call to `map`
@@ -8,22 +8,34 @@ LL | let _ = x.into_iter().map(|Foo { foo, bar }| Foo { foo, bar });
88
= help: to override `-D warnings` add `#[allow(clippy::map_identity)]`
99

1010
error: unnecessary map of the identity function
11-
--> tests/ui/f1.rs:25:26
11+
--> tests/ui/f1.rs:30:26
1212
|
1313
LL | let _ = x.into_iter().map(|Foo { foo, bar }| foo::Foo { foo, bar });
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove the call to `map`
1515

1616
error: unnecessary map of the identity function
17-
--> tests/ui/f1.rs:33:26
17+
--> tests/ui/f1.rs:38:26
1818
|
1919
LL | let _ = x.into_iter().map(|Foo { foo, bar }| Foo { foo: foo, bar: bar });
2020
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove the call to `map`
2121

2222
error: unnecessary map of the identity function
23-
--> tests/ui/f1.rs:37:26
23+
--> tests/ui/f1.rs:42:26
2424
|
2525
LL | let _ = x.into_iter().map(|Foo { foo, bar }| Foo { bar, foo });
2626
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove the call to `map`
2727

28-
error: aborting due to 4 previous errors
28+
error: unnecessary map of the identity function
29+
--> tests/ui/f1.rs:52:26
30+
|
31+
LL | let _ = x.into_iter().map(|Foo2(foo, bar)| Foo2(foo, bar));
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove the call to `map`
33+
34+
error: unnecessary map of the identity function
35+
--> tests/ui/f1.rs:56:26
36+
|
37+
LL | let _ = x.into_iter().map(|Foo2(foo, bar)| foo::Foo2(foo, bar));
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove the call to `map`
39+
40+
error: aborting due to 6 previous errors
2941

0 commit comments

Comments
 (0)