Skip to content

Commit

Permalink
Lint using identity into_iter conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
aaudiber committed Jul 30, 2018
1 parent 4010788 commit 0ea1afa
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
14 changes: 12 additions & 2 deletions clippy_lints/src/identity_conversion.rs
Expand Up @@ -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.
///
Expand All @@ -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)]
Expand Down Expand Up @@ -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, "<expr>").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 {
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/identity_conversion.rs
Expand Up @@ -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();
}
26 changes: 19 additions & 7 deletions tests/ui/identity_conversion.stderr
Expand Up @@ -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

0 comments on commit 0ea1afa

Please sign in to comment.