Skip to content

Commit 9168746

Browse files
committed
Corrected explicit_counter_loop behavior with nested loops
1 parent 53c2620 commit 9168746

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

clippy_lints/src/loops.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,8 +1942,7 @@ impl<'a, 'tcx> Visitor<'tcx> for IncrementVisitor<'a, 'tcx> {
19421942
}
19431943
}
19441944
} else if is_loop(expr) {
1945-
self.states.clear();
1946-
self.done = true;
1945+
walk_expr(self, expr);
19471946
return;
19481947
} else if is_conditional(expr) {
19491948
self.depth += 1;

tests/ui/for_loop.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,5 +612,27 @@ mod issue_1219 {
612612
}
613613
println!("{}", count);
614614
}
615+
616+
// should trigger the lint because the count is not conditional
617+
let text = "banana";
618+
let mut count = 0;
619+
for ch in text.chars() {
620+
count += 1;
621+
for i in 0..2 {
622+
let _ = 123;
623+
}
624+
println!("{}", count);
625+
}
626+
627+
// should not trigger the lint because the count is incremented multiple times
628+
let text = "banana";
629+
let mut count = 0;
630+
for ch in text.chars() {
631+
count += 1;
632+
for i in 0..2 {
633+
count += 1;
634+
}
635+
println!("{}", count);
636+
}
615637
}
616638
}

tests/ui/for_loop.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,5 +493,11 @@ error: the variable `count` is used as a loop counter. Consider using `for (coun
493493
608 | for ch in text.chars() {
494494
| ^^^^^^^^^^^^
495495

496-
error: aborting due to 60 previous errors
496+
error: the variable `count` is used as a loop counter. Consider using `for (count, item) in text.chars().enumerate()` or similar iterators
497+
--> $DIR/for_loop.rs:619:19
498+
|
499+
619 | for ch in text.chars() {
500+
| ^^^^^^^^^^^^
501+
502+
error: aborting due to 61 previous errors
497503

0 commit comments

Comments
 (0)