Skip to content

Commit ce55426

Browse files
committed
Updated explicit_counter_loop tests based on discussion in #3135
1 parent edfa9fe commit ce55426

File tree

2 files changed

+16
-27
lines changed

2 files changed

+16
-27
lines changed

clippy_lints/src/loops.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1996,9 +1996,6 @@ impl<'a, 'tcx> Visitor<'tcx> for InitializeVisitor<'a, 'tcx> {
19961996
if self.state == VarState::DontWarn {
19971997
return;
19981998
}
1999-
if self.past_loop {
2000-
return;
2001-
}
20021999
if SpanlessEq::new(self.cx).eq_expr(&expr, self.end_expr) {
20032000
self.past_loop = true;
20042001
return;
@@ -2027,7 +2024,12 @@ impl<'a, 'tcx> Visitor<'tcx> for InitializeVisitor<'a, 'tcx> {
20272024
_ => (),
20282025
}
20292026
}
2030-
} else if is_loop(expr) {
2027+
2028+
if self.past_loop {
2029+
self.state = VarState::DontWarn;
2030+
return;
2031+
}
2032+
} else if !self.past_loop && is_loop(expr) {
20312033
self.state = VarState::DontWarn;
20322034
return;
20332035
} else if is_conditional(expr) {

tests/ui/for_loop.rs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -575,44 +575,31 @@ mod issue_2496 {
575575
mod issue_1219 {
576576
#[warn(clippy::explicit_counter_loop)]
577577
pub fn test() {
578-
// should not trigger the lint, because of the continue statement
579-
let text = "banana";
580-
let mut count = 0;
581-
for ch in text.chars() {
582-
if ch == 'a' {
583-
continue;
584-
}
585-
count += 1;
586-
}
587-
println!("{}", count);
578+
// should not trigger the lint because variable is used after the loop #473
579+
let vec = vec![1,2,3];
580+
let mut index = 0;
581+
for _v in &vec { index += 1 }
582+
println!("index: {}", index);
588583

589-
// should trigger the lint
584+
// should not trigger the lint because the count is conditional #1219
590585
let text = "banana";
591586
let mut count = 0;
592587
for ch in text.chars() {
593588
if ch == 'a' {
594-
println!("abc")
589+
continue;
595590
}
596591
count += 1;
592+
println!("{}", count);
597593
}
598-
println!("{}", count);
599594

600-
// should not trigger the lint
595+
// should not trigger the lint because the count is conditional
601596
let text = "banana";
602597
let mut count = 0;
603598
for ch in text.chars() {
604599
if ch == 'a' {
605600
count += 1;
606601
}
602+
println!("{}", count);
607603
}
608-
println!("{}", count);
609-
610-
// should trigger the lint
611-
let text = "banana";
612-
let mut count = 0;
613-
for _ch in text.chars() {
614-
count += 1;
615-
}
616-
println!("{}", count);
617604
}
618605
}

0 commit comments

Comments
 (0)