Fix: Don't show lint for types that doesn't implement Iterator

This commit is contained in:
Piti the little Light 2020-10-12 17:27:50 +02:00 committed by flip1995
parent f359fb872b
commit 52d1ea3c9a
No known key found for this signature in database
GPG Key ID: AFC0354489087877
2 changed files with 7 additions and 2 deletions

View File

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

View File

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