about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2019-11-11 19:24:20 +0000
committerbors <bors@rust-lang.org>2019-11-11 19:24:20 +0000
commit2646b108d5900786f1efb6a56d18ca9b951bd015 (patch)
treec56159e2cd2776b36678e7dbb58212fcfc6938b9
parent86b8643586aa39f36fb7a02e98c8d64d31415e70 (diff)
parentc88afce6fc59dc6683871c4d2a7463a94b6fb0c5 (diff)
downloadrust-2646b108d5900786f1efb6a56d18ca9b951bd015.tar.gz
rust-2646b108d5900786f1efb6a56d18ca9b951bd015.zip
Auto merge of #4803 - tomprogrammer:issue-4732, r=phansch
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.

changelog: Fix false positive in `explicit_counter_loop`

Fixes #4732
-rw-r--r--clippy_lints/src/loops.rs3
-rw-r--r--tests/ui/explicit_counter_loop.rs13
2 files changed, 15 insertions, 1 deletions
diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs
index fcdef51c1d4..75d540b38e5 100644
--- a/clippy_lints/src/loops.rs
+++ b/clippy_lints/src/loops.rs
@@ -2194,8 +2194,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);
+    }
+}