Skip to content

Commit 2646b10

Browse files
committed
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
2 parents 86b8643 + c88afce commit 2646b10

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

clippy_lints/src/loops.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2194,8 +2194,9 @@ impl<'a, 'tcx> Visitor<'tcx> for InitializeVisitor<'a, 'tcx> {
21942194
}
21952195
walk_expr(self, expr);
21962196
}
2197+
21972198
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
2198-
NestedVisitorMap::None
2199+
NestedVisitorMap::OnlyBodies(&self.cx.tcx.hir())
21992200
}
22002201
}
22012202

tests/ui/explicit_counter_loop.rs

+13
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,16 @@ mod issue_1670 {
132132
}
133133
}
134134
}
135+
136+
mod issue_4732 {
137+
pub fn test() {
138+
let slice = &[1, 2, 3];
139+
let mut index = 0;
140+
141+
// should not trigger the lint because the count is used after the loop
142+
for _v in slice {
143+
index += 1
144+
}
145+
let _closure = || println!("index: {}", index);
146+
}
147+
}

0 commit comments

Comments
 (0)