Corrected explicit_counter_loop missing lints if variable used after loop

This commit is contained in:
Josh Mcguigan 2018-09-06 06:20:25 -07:00
parent 4b668159d2
commit edfa9feac2
2 changed files with 36 additions and 9 deletions

View file

@ -1996,6 +1996,9 @@ impl<'a, 'tcx> Visitor<'tcx> for InitializeVisitor<'a, 'tcx> {
if self.state == VarState::DontWarn {
return;
}
if self.past_loop {
return;
}
if SpanlessEq::new(self.cx).eq_expr(&expr, self.end_expr) {
self.past_loop = true;
return;
@ -2024,12 +2027,7 @@ impl<'a, 'tcx> Visitor<'tcx> for InitializeVisitor<'a, 'tcx> {
_ => (),
}
}
if self.past_loop {
self.state = VarState::DontWarn;
return;
}
} else if !self.past_loop && is_loop(expr) {
} else if is_loop(expr) {
self.state = VarState::DontWarn;
return;
} else if is_conditional(expr) {

View file

@ -573,16 +573,45 @@ mod issue_2496 {
}
mod issue_1219 {
// potential false positive for explicit_counter_loop
#[warn(clippy::explicit_counter_loop)]
pub fn test() {
let thing = 5;
// should not trigger the lint, because of the continue statement
let text = "banana";
let mut count = 0;
for ch in text.chars() {
if ch == 'a' {
continue;
}
count += 1
count += 1;
}
println!("{}", count);
// should trigger the lint
let text = "banana";
let mut count = 0;
for ch in text.chars() {
if ch == 'a' {
println!("abc")
}
count += 1;
}
println!("{}", count);
// should not trigger the lint
let text = "banana";
let mut count = 0;
for ch in text.chars() {
if ch == 'a' {
count += 1;
}
}
println!("{}", count);
// should trigger the lint
let text = "banana";
let mut count = 0;
for _ch in text.chars() {
count += 1;
}
println!("{}", count);
}