From eadd9d24dcd7c84c96dcd17234b8aadf9a113bd3 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Sat, 25 Apr 2020 20:51:02 +0200 Subject: [PATCH] Don't trigger while_let_on_iterator when the iterator is recreated every iteration --- clippy_lints/src/loops.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index 56dd2795c60..6e9b7f685eb 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -566,6 +566,17 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) { ) = (pat, &match_expr.kind) { let iter_expr = &method_args[0]; + + // Don't lint when the iterator is recreated on every iteration + if_chain! { + if let ExprKind::MethodCall(..) | ExprKind::Call(..) = iter_expr.kind; + if let Some(iter_def_id) = get_trait_def_id(cx, &paths::ITERATOR); + if implements_trait(cx, cx.tables.expr_ty(iter_expr), iter_def_id, &[]); + then { + return; + } + } + let lhs_constructor = last_path_segment(qpath); if method_path.ident.name == sym!(next) && match_trait_method(cx, match_expr, &paths::ITERATOR)