]> git.lizzy.rs Git - rust.git/commitdiff
explicit_counter_loop fix #3308 false positive
authorJosh Mcguigan <joshmcg88@gmail.com>
Sat, 13 Oct 2018 13:57:52 +0000 (06:57 -0700)
committerJosh Mcguigan <joshmcg88@gmail.com>
Sat, 13 Oct 2018 13:57:52 +0000 (06:57 -0700)
clippy_lints/src/loops.rs
tests/ui/for_loop.rs

index b807e4fb9e1ced41dafa2a3c8705e80b012ecb47..064a8d5229d8bf84d8e3143245a2031d521f3947 100644 (file)
@@ -1952,10 +1952,7 @@ fn visit_expr(&mut self, expr: &'tcx Expr) {
                     _ => (),
                 }
             }
-        } else if is_loop(expr) {
-            walk_expr(self, expr);
-            return;
-        } else if is_conditional(expr) {
+        } else if is_loop(expr) || is_conditional(expr) {
             self.depth += 1;
             walk_expr(self, expr);
             self.depth -= 1;
index bdb6b56e0bb33ccddb7818c628f7f007e44d0816..89c452f44df010d0b3d6ddf6f879cba098e22c57 100644 (file)
@@ -646,3 +646,38 @@ pub fn test() {
         }
     }
 }
+
+mod issue_3308 {
+    #[warn(clippy::explicit_counter_loop)]
+    pub fn test() {
+        // should not trigger the lint because the count is incremented multiple times
+        let mut skips = 0;
+        let erasures = vec![];
+        for i in 0..10 {
+            while erasures.contains(&(i + skips)) {
+                skips += 1;
+            }
+            println!("{}", skips);
+        }
+
+        // should not trigger the lint because the count is incremented multiple times
+        let mut skips = 0;
+        for i in 0..10 {
+            let mut j = 0;
+            while j < 5 {
+                skips += 1;
+                j += 1;
+            }
+            println!("{}", skips);
+        }
+
+        // should not trigger the lint because the count is incremented multiple times
+        let mut skips = 0;
+        for i in 0..10 {
+            for j in 0..5 {
+                skips += 1;
+            }
+            println!("{}", skips);
+        }
+    }
+}