Skip to content

Commit 601cc9d

Browse files
authored
Merge pull request rust-lang#3310 from JoshMcguigan/explicit_counter_loop-3308
explicit_counter_loop fix rust-lang#3308 false positive
2 parents 6812c0c + c6f79c7 commit 601cc9d

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

clippy_lints/src/loops.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1952,10 +1952,7 @@ impl<'a, 'tcx> Visitor<'tcx> for IncrementVisitor<'a, 'tcx> {
19521952
_ => (),
19531953
}
19541954
}
1955-
} else if is_loop(expr) {
1956-
walk_expr(self, expr);
1957-
return;
1958-
} else if is_conditional(expr) {
1955+
} else if is_loop(expr) || is_conditional(expr) {
19591956
self.depth += 1;
19601957
walk_expr(self, expr);
19611958
self.depth -= 1;

tests/ui/for_loop.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,3 +646,38 @@ mod issue_1219 {
646646
}
647647
}
648648
}
649+
650+
mod issue_3308 {
651+
#[warn(clippy::explicit_counter_loop)]
652+
pub fn test() {
653+
// should not trigger the lint because the count is incremented multiple times
654+
let mut skips = 0;
655+
let erasures = vec![];
656+
for i in 0..10 {
657+
while erasures.contains(&(i + skips)) {
658+
skips += 1;
659+
}
660+
println!("{}", skips);
661+
}
662+
663+
// should not trigger the lint because the count is incremented multiple times
664+
let mut skips = 0;
665+
for i in 0..10 {
666+
let mut j = 0;
667+
while j < 5 {
668+
skips += 1;
669+
j += 1;
670+
}
671+
println!("{}", skips);
672+
}
673+
674+
// should not trigger the lint because the count is incremented multiple times
675+
let mut skips = 0;
676+
for i in 0..10 {
677+
for j in 0..5 {
678+
skips += 1;
679+
}
680+
println!("{}", skips);
681+
}
682+
}
683+
}

0 commit comments

Comments
 (0)