From edfa9feac2d86b4c775c12b155b06efe1efe9d5a Mon Sep 17 00:00:00 2001 From: Josh Mcguigan Date: Thu, 6 Sep 2018 06:20:25 -0700 Subject: [PATCH] Corrected explicit_counter_loop missing lints if variable used after loop --- clippy_lints/src/loops.rs | 10 ++++------ tests/ui/for_loop.rs | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index 8a12530cb0d2..6dfe0a7ec014 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -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) { diff --git a/tests/ui/for_loop.rs b/tests/ui/for_loop.rs index 76bc96f91b7f..286ef190cbf3 100644 --- a/tests/ui/for_loop.rs +++ b/tests/ui/for_loop.rs @@ -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); }