diff options
| author | Piti the little Light <djhlinacz@gmail.com> | 2020-10-12 17:27:50 +0200 |
|---|---|---|
| committer | flip1995 <philipp.krones@embecosm.com> | 2020-11-03 16:44:24 +0100 |
| commit | 52d1ea3c9ad8ea97350ba7a0ca0a8e172cfcae78 (patch) | |
| tree | 82aff92646e7eeaaf3dd42da8843c87511b9ce95 | |
| parent | f359fb872b405fca196f40eadd341d1d06f1fb8b (diff) | |
| download | rust-52d1ea3c9ad8ea97350ba7a0ca0a8e172cfcae78.tar.gz rust-52d1ea3c9ad8ea97350ba7a0ca0a8e172cfcae78.zip | |
Fix: Don't show lint for types that doesn't implement Iterator
| -rw-r--r-- | clippy_lints/src/methods/mod.rs | 7 | ||||
| -rw-r--r-- | tests/ui/from_iter_instead_of_collect.rs | 2 |
2 files changed, 7 insertions, 2 deletions
diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index fde43f0055d..521b151f5e1 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -3874,9 +3874,12 @@ fn lint_filetype_is_file(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir fn lint_from_iter(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) { let ty = cx.typeck_results().expr_ty(expr); - let id = get_trait_def_id(cx, &paths::FROM_ITERATOR).unwrap(); + let arg_ty = cx.typeck_results().expr_ty(&args[0]); - if implements_trait(cx, ty, id, &[]) { + let from_iter_id = get_trait_def_id(cx, &paths::FROM_ITERATOR).unwrap(); + let iter_id = get_trait_def_id(cx, &paths::ITERATOR).unwrap(); + + if implements_trait(cx, ty, from_iter_id, &[]) && implements_trait(cx, arg_ty, iter_id, &[]) { // `expr` implements `FromIterator` trait let iter_expr = snippet(cx, args[0].span, ".."); span_lint_and_sugg( diff --git a/tests/ui/from_iter_instead_of_collect.rs b/tests/ui/from_iter_instead_of_collect.rs index 25b87a0a903..045eb3133d3 100644 --- a/tests/ui/from_iter_instead_of_collect.rs +++ b/tests/ui/from_iter_instead_of_collect.rs @@ -8,4 +8,6 @@ fn main() { Vec::from_iter(iter_expr); HashMap::<usize, &i8>::from_iter(vec![5, 5, 5, 5].iter().enumerate()); + + Vec::from_iter(vec![42u32]); } |
