Updated explicit_counter_loop tests based on discussion in #3135

This commit is contained in:
Josh Mcguigan 2018-09-07 05:32:56 -07:00
parent edfa9feac2
commit ce554267b8
2 changed files with 16 additions and 27 deletions

View file

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

View file

@ -575,7 +575,13 @@ mod issue_2496 {
mod issue_1219 {
#[warn(clippy::explicit_counter_loop)]
pub fn test() {
// should not trigger the lint, because of the continue statement
// should not trigger the lint because variable is used after the loop #473
let vec = vec![1,2,3];
let mut index = 0;
for _v in &vec { index += 1 }
println!("index: {}", index);
// should not trigger the lint because the count is conditional #1219
let text = "banana";
let mut count = 0;
for ch in text.chars() {
@ -583,36 +589,17 @@ mod issue_1219 {
continue;
}
count += 1;
println!("{}", count);
}
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
// should not trigger the lint because the count is conditional
let text = "banana";
let mut count = 0;
for ch in text.chars() {
if ch == 'a' {
count += 1;
}
println!("{}", count);
}
println!("{}", count);
// should trigger the lint
let text = "banana";
let mut count = 0;
for _ch in text.chars() {
count += 1;
}
println!("{}", count);
}
}