Skip to content

Commit c88afce

Browse files
author
Thomas Bahn
committed
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.
1 parent 338f5e6 commit c88afce

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

clippy_lints/src/loops.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2192,8 +2192,9 @@ impl<'a, 'tcx> Visitor<'tcx> for InitializeVisitor<'a, 'tcx> {
21922192
}
21932193
walk_expr(self, expr);
21942194
}
2195+
21952196
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
2196-
NestedVisitorMap::None
2197+
NestedVisitorMap::OnlyBodies(&self.cx.tcx.hir())
21972198
}
21982199
}
21992200

tests/ui/explicit_counter_loop.rs

Lines changed: 13 additions & 0 deletions
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)