diff options
| author | Thomas Bahn <thomas@thomas-bahn.net> | 2019-11-11 11:27:18 +0100 |
|---|---|---|
| committer | Thomas Bahn <thomas@thomas-bahn.net> | 2019-11-11 11:36:53 +0100 |
| commit | c88afce6fc59dc6683871c4d2a7463a94b6fb0c5 (patch) | |
| tree | 434878eebbc002aaafc0812a36d65a9983810db9 | |
| parent | 338f5e6801315fae7353facf5a338c78df7b0bce (diff) | |
| download | rust-c88afce6fc59dc6683871c4d2a7463a94b6fb0c5.tar.gz rust-c88afce6fc59dc6683871c4d2a7463a94b6fb0c5.zip | |
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.
| -rw-r--r-- | clippy_lints/src/loops.rs | 3 | ||||
| -rw-r--r-- | tests/ui/explicit_counter_loop.rs | 13 |
2 files changed, 15 insertions, 1 deletions
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); + } +} |
