Merge pull request #2975 from aaudiber/lint-identity-into-iter

Lint using identity into_iter conversion
This commit is contained in:
Philipp Hansch 2018-08-05 20:38:28 +01:00 committed by GitHub
commit 32e4897854
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 9 deletions

View file

@ -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, "<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 {

View file

@ -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();
}

View file

@ -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