Skip to content

Commit

Permalink
Fix: Don't show lint for types that doesn't implement Iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
pitiK3U authored and flip1995 committed Nov 3, 2020
1 parent f359fb8 commit 52d1ea3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 5 additions & 2 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/from_iter_instead_of_collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

0 comments on commit 52d1ea3

Please sign in to comment.