From c88afce6fc59dc6683871c4d2a7463a94b6fb0c5 Mon Sep 17 00:00:00 2001 From: Thomas Bahn Date: Mon, 11 Nov 2019 11:27:18 +0100 Subject: [PATCH] Fix false positive in explicit_counter_loop lint When the counter was used in a closure after the loop the lint didn't detect the usage of the counter correctly. --- clippy_lints/src/loops.rs | 3 ++- tests/ui/explicit_counter_loop.rs | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index 805e609cea6..958545b01d2 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -2192,8 +2192,9 @@ impl<'a, 'tcx> Visitor<'tcx> for InitializeVisitor<'a, 'tcx> { } walk_expr(self, expr); } + fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> { - NestedVisitorMap::None + NestedVisitorMap::OnlyBodies(&self.cx.tcx.hir()) } } diff --git a/tests/ui/explicit_counter_loop.rs b/tests/ui/explicit_counter_loop.rs index e6fbf83a287..aa6ef162fe4 100644 --- a/tests/ui/explicit_counter_loop.rs +++ b/tests/ui/explicit_counter_loop.rs @@ -132,3 +132,16 @@ mod issue_1670 { } } } + +mod issue_4732 { + pub fn test() { + let slice = &[1, 2, 3]; + let mut index = 0; + + // should not trigger the lint because the count is used after the loop + for _v in slice { + index += 1 + } + let _closure = || println!("index: {}", index); + } +}