Skip to content

Commit edfa9fe

Browse files
committed
Corrected explicit_counter_loop missing lints if variable used after loop
1 parent 4b66815 commit edfa9fe

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

clippy_lints/src/loops.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1996,6 +1996,9 @@ 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+
}
19992002
if SpanlessEq::new(self.cx).eq_expr(&expr, self.end_expr) {
20002003
self.past_loop = true;
20012004
return;
@@ -2024,12 +2027,7 @@ impl<'a, 'tcx> Visitor<'tcx> for InitializeVisitor<'a, 'tcx> {
20242027
_ => (),
20252028
}
20262029
}
2027-
2028-
if self.past_loop {
2029-
self.state = VarState::DontWarn;
2030-
return;
2031-
}
2032-
} else if !self.past_loop && is_loop(expr) {
2030+
} else if is_loop(expr) {
20332031
self.state = VarState::DontWarn;
20342032
return;
20352033
} else if is_conditional(expr) {

tests/ui/for_loop.rs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -573,16 +573,45 @@ mod issue_2496 {
573573
}
574574

575575
mod issue_1219 {
576-
// potential false positive for explicit_counter_loop
576+
#[warn(clippy::explicit_counter_loop)]
577577
pub fn test() {
578-
let thing = 5;
578+
// should not trigger the lint, because of the continue statement
579579
let text = "banana";
580580
let mut count = 0;
581581
for ch in text.chars() {
582582
if ch == 'a' {
583583
continue;
584584
}
585-
count += 1
585+
count += 1;
586+
}
587+
println!("{}", count);
588+
589+
// should trigger the lint
590+
let text = "banana";
591+
let mut count = 0;
592+
for ch in text.chars() {
593+
if ch == 'a' {
594+
println!("abc")
595+
}
596+
count += 1;
597+
}
598+
println!("{}", count);
599+
600+
// should not trigger the lint
601+
let text = "banana";
602+
let mut count = 0;
603+
for ch in text.chars() {
604+
if ch == 'a' {
605+
count += 1;
606+
}
607+
}
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;
586615
}
587616
println!("{}", count);
588617
}

0 commit comments

Comments
 (0)