diff --git a/clippy_lints/src/identity_conversion.rs b/clippy_lints/src/identity_conversion.rs index a0705f62544..cf05583d85e 100644 --- a/clippy_lints/src/identity_conversion.rs +++ b/clippy_lints/src/identity_conversion.rs @@ -5,7 +5,7 @@ use syntax::ast::NodeId; use crate::utils::{in_macro, match_def_path, match_trait_method, same_tys, snippet, span_lint_and_then}; use crate::utils::{opt_def_id, paths, resolve_node}; -/// **What it does:** Checks for always-identical `Into`/`From` conversions. +/// **What it does:** Checks for always-identical `Into`/`From`/`IntoIter` conversions. /// /// **Why is this bad?** Redundant code. /// @@ -19,7 +19,7 @@ use crate::utils::{opt_def_id, paths, resolve_node}; declare_clippy_lint! { pub IDENTITY_CONVERSION, complexity, - "using always-identical `Into`/`From` conversions" + "using always-identical `Into`/`From`/`IntoIter` conversions" } #[derive(Default)] @@ -67,6 +67,16 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion { }); } } + if match_trait_method(cx, e, &paths::INTO_ITERATOR) && &*name.ident.as_str() == "into_iter" { + let a = cx.tables.expr_ty(e); + let b = cx.tables.expr_ty(&args[0]); + if same_tys(cx, a, b) { + let sugg = snippet(cx, args[0].span, "").into_owned(); + span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| { + db.span_suggestion(e.span, "consider removing `.into_iter()`", sugg); + }); + } + } }, ExprKind::Call(ref path, ref args) => if let ExprKind::Path(ref qpath) = path.node { diff --git a/tests/ui/identity_conversion.rs b/tests/ui/identity_conversion.rs index d254b746d79..9ab81f1b1cb 100644 --- a/tests/ui/identity_conversion.rs +++ b/tests/ui/identity_conversion.rs @@ -32,9 +32,12 @@ fn main() { { let _: String = "foo".into(); let _ = String::from("foo"); + let _ = "".lines().into_iter(); } let _: String = "foo".to_string().into(); let _: String = From::from("foo".to_string()); let _ = String::from("foo".to_string()); + let _ = "".lines().into_iter(); + let _ = vec![1, 2, 3].into_iter().into_iter(); } diff --git a/tests/ui/identity_conversion.stderr b/tests/ui/identity_conversion.stderr index 1ae3f229dd8..f7993d69c50 100644 --- a/tests/ui/identity_conversion.stderr +++ b/tests/ui/identity_conversion.stderr @@ -23,22 +23,34 @@ error: identical conversion | ^^^^^^^^^^^ help: consider removing `.into()`: `0i32` error: identical conversion - --> $DIR/identity_conversion.rs:37:21 + --> $DIR/identity_conversion.rs:38:21 | -37 | let _: String = "foo".to_string().into(); +38 | let _: String = "foo".to_string().into(); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `"foo".to_string()` error: identical conversion - --> $DIR/identity_conversion.rs:38:21 + --> $DIR/identity_conversion.rs:39:21 | -38 | let _: String = From::from("foo".to_string()); +39 | let _: String = From::from("foo".to_string()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `From::from()`: `"foo".to_string()` error: identical conversion - --> $DIR/identity_conversion.rs:39:13 + --> $DIR/identity_conversion.rs:40:13 | -39 | let _ = String::from("foo".to_string()); +40 | let _ = String::from("foo".to_string()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `String::from()`: `"foo".to_string()` -error: aborting due to 6 previous errors +error: identical conversion + --> $DIR/identity_conversion.rs:41:13 + | +41 | let _ = "".lines().into_iter(); + | ^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `"".lines()` + +error: identical conversion + --> $DIR/identity_conversion.rs:42:13 + | +42 | let _ = vec![1, 2, 3].into_iter().into_iter(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `vec![1, 2, 3].into_iter()` + +error: aborting due to 8 previous errors