From 53c262048c4d7cbffd32074f8037430ca803fbff Mon Sep 17 00:00:00 2001 From: Josh Mcguigan Date: Fri, 7 Sep 2018 19:58:19 -0700 Subject: [PATCH] Fix #1219 false positive for explicit_counter_loop --- clippy_lints/src/loops.rs | 3 +++ tests/ui/for_loop.rs | 11 +++++++++++ tests/ui/for_loop.stderr | 8 +++++++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index 8a12530cb0d2..091b477e33b4 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -1950,6 +1950,9 @@ impl<'a, 'tcx> Visitor<'tcx> for IncrementVisitor<'a, 'tcx> { walk_expr(self, expr); self.depth -= 1; return; + } else if let ExprKind::Continue(_) = expr.node { + self.done = true; + return; } walk_expr(self, expr); } diff --git a/tests/ui/for_loop.rs b/tests/ui/for_loop.rs index 029b6f7aa209..a53b9cd2874e 100644 --- a/tests/ui/for_loop.rs +++ b/tests/ui/for_loop.rs @@ -601,5 +601,16 @@ mod issue_1219 { } println!("{}", count); } + + // should trigger the lint because the count is not conditional + let text = "banana"; + let mut count = 0; + for ch in text.chars() { + count += 1; + if ch == 'a' { + continue; + } + println!("{}", count); + } } } diff --git a/tests/ui/for_loop.stderr b/tests/ui/for_loop.stderr index bab7bdc77a4c..bbd663dc8524 100644 --- a/tests/ui/for_loop.stderr +++ b/tests/ui/for_loop.stderr @@ -487,5 +487,11 @@ error: it looks like you're manually copying between slices 547 | for i in 0..src.len() { | ^^^^^^^^^^^^ help: try replacing the loop by: `dst[..src.len()].clone_from_slice(&src[..])` -error: aborting due to 59 previous errors +error: the variable `count` is used as a loop counter. Consider using `for (count, item) in text.chars().enumerate()` or similar iterators + --> $DIR/for_loop.rs:608:19 + | +608 | for ch in text.chars() { + | ^^^^^^^^^^^^ + +error: aborting due to 60 previous errors