Skip to content

Commit 53c2620

Browse files
committed
Fix #1219 false positive for explicit_counter_loop
1 parent ce55426 commit 53c2620

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

clippy_lints/src/loops.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1950,6 +1950,9 @@ impl<'a, 'tcx> Visitor<'tcx> for IncrementVisitor<'a, 'tcx> {
19501950
walk_expr(self, expr);
19511951
self.depth -= 1;
19521952
return;
1953+
} else if let ExprKind::Continue(_) = expr.node {
1954+
self.done = true;
1955+
return;
19531956
}
19541957
walk_expr(self, expr);
19551958
}

tests/ui/for_loop.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,5 +601,16 @@ mod issue_1219 {
601601
}
602602
println!("{}", count);
603603
}
604+
605+
// should trigger the lint because the count is not conditional
606+
let text = "banana";
607+
let mut count = 0;
608+
for ch in text.chars() {
609+
count += 1;
610+
if ch == 'a' {
611+
continue;
612+
}
613+
println!("{}", count);
614+
}
604615
}
605616
}

tests/ui/for_loop.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,5 +487,11 @@ error: it looks like you're manually copying between slices
487487
547 | for i in 0..src.len() {
488488
| ^^^^^^^^^^^^ help: try replacing the loop by: `dst[..src.len()].clone_from_slice(&src[..])`
489489

490-
error: aborting due to 59 previous errors
490+
error: the variable `count` is used as a loop counter. Consider using `for (count, item) in text.chars().enumerate()` or similar iterators
491+
--> $DIR/for_loop.rs:608:19
492+
|
493+
608 | for ch in text.chars() {
494+
| ^^^^^^^^^^^^
495+
496+
error: aborting due to 60 previous errors
491497

0 commit comments

Comments
 (0)