Skip to content

Commit 32e4897

Browse files
authored
Merge pull request #2975 from aaudiber/lint-identity-into-iter
Lint using identity into_iter conversion
2 parents a016d4e + 0ea1afa commit 32e4897

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

clippy_lints/src/identity_conversion.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use syntax::ast::NodeId;
55
use crate::utils::{in_macro, match_def_path, match_trait_method, same_tys, snippet, span_lint_and_then};
66
use crate::utils::{opt_def_id, paths, resolve_node};
77

8-
/// **What it does:** Checks for always-identical `Into`/`From` conversions.
8+
/// **What it does:** Checks for always-identical `Into`/`From`/`IntoIter` conversions.
99
///
1010
/// **Why is this bad?** Redundant code.
1111
///
@@ -19,7 +19,7 @@ use crate::utils::{opt_def_id, paths, resolve_node};
1919
declare_clippy_lint! {
2020
pub IDENTITY_CONVERSION,
2121
complexity,
22-
"using always-identical `Into`/`From` conversions"
22+
"using always-identical `Into`/`From`/`IntoIter` conversions"
2323
}
2424

2525
#[derive(Default)]
@@ -67,6 +67,16 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
6767
});
6868
}
6969
}
70+
if match_trait_method(cx, e, &paths::INTO_ITERATOR) && &*name.ident.as_str() == "into_iter" {
71+
let a = cx.tables.expr_ty(e);
72+
let b = cx.tables.expr_ty(&args[0]);
73+
if same_tys(cx, a, b) {
74+
let sugg = snippet(cx, args[0].span, "<expr>").into_owned();
75+
span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| {
76+
db.span_suggestion(e.span, "consider removing `.into_iter()`", sugg);
77+
});
78+
}
79+
}
7080
},
7181

7282
ExprKind::Call(ref path, ref args) => if let ExprKind::Path(ref qpath) = path.node {

tests/ui/identity_conversion.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ fn main() {
3232
{
3333
let _: String = "foo".into();
3434
let _ = String::from("foo");
35+
let _ = "".lines().into_iter();
3536
}
3637

3738
let _: String = "foo".to_string().into();
3839
let _: String = From::from("foo".to_string());
3940
let _ = String::from("foo".to_string());
41+
let _ = "".lines().into_iter();
42+
let _ = vec![1, 2, 3].into_iter().into_iter();
4043
}

tests/ui/identity_conversion.stderr

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,34 @@ error: identical conversion
2323
| ^^^^^^^^^^^ help: consider removing `.into()`: `0i32`
2424

2525
error: identical conversion
26-
--> $DIR/identity_conversion.rs:37:21
26+
--> $DIR/identity_conversion.rs:38:21
2727
|
28-
37 | let _: String = "foo".to_string().into();
28+
38 | let _: String = "foo".to_string().into();
2929
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `"foo".to_string()`
3030

3131
error: identical conversion
32-
--> $DIR/identity_conversion.rs:38:21
32+
--> $DIR/identity_conversion.rs:39:21
3333
|
34-
38 | let _: String = From::from("foo".to_string());
34+
39 | let _: String = From::from("foo".to_string());
3535
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `From::from()`: `"foo".to_string()`
3636

3737
error: identical conversion
38-
--> $DIR/identity_conversion.rs:39:13
38+
--> $DIR/identity_conversion.rs:40:13
3939
|
40-
39 | let _ = String::from("foo".to_string());
40+
40 | let _ = String::from("foo".to_string());
4141
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `String::from()`: `"foo".to_string()`
4242

43-
error: aborting due to 6 previous errors
43+
error: identical conversion
44+
--> $DIR/identity_conversion.rs:41:13
45+
|
46+
41 | let _ = "".lines().into_iter();
47+
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `"".lines()`
48+
49+
error: identical conversion
50+
--> $DIR/identity_conversion.rs:42:13
51+
|
52+
42 | let _ = vec![1, 2, 3].into_iter().into_iter();
53+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `vec![1, 2, 3].into_iter()`
54+
55+
error: aborting due to 8 previous errors
4456

0 commit comments

Comments
 (0)