Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions clippy_lints/src/loops/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ impl<'a, 'tcx> IncrementVisitor<'a, 'tcx> {
}

impl<'tcx> Visitor<'tcx> for IncrementVisitor<'_, 'tcx> {
fn visit_local(&mut self, l: &'tcx LetStmt<'tcx>) {
if let Some(init) = l.init {
self.visit_expr(init);
if let Some(els) = l.els {
self.depth += 1;
self.visit_block(els);
self.depth -= 1;
}
}
}

fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
// If node is a variable
if let Some(def_id) = expr.res_local_id() {
Expand Down
21 changes: 21 additions & 0 deletions tests/ui/explicit_counter_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,24 @@ pub fn issue_16642() {
base += 1;
}
}

fn issue_17014(v: Vec<u8>) {
let mut count = 0;
for item in &v {
let Some(_) = Some(0) else {
count += 1;
continue;
};
}

let mut count = 0;
for item in &v {
//~^ explicit_counter_loop
let Some(_) = ({
count += 1;
Some(0)
}) else {
continue;
};
}
}
8 changes: 7 additions & 1 deletion tests/ui/explicit_counter_loop.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,11 @@ error: the variable `base` is used as a loop counter
LL | for _ in 5..MAX {
| ^^^^^^^^^^^^^^^ help: consider using: `for (base, _) in (100..).zip((5..MAX))`

error: aborting due to 17 previous errors
error: the variable `count` is used as a loop counter
--> tests/ui/explicit_counter_loop.rs:377:5
|
LL | for item in &v {
| ^^^^^^^^^^^^^^ help: consider using: `for (count, item) in v.iter().enumerate()`

error: aborting due to 18 previous errors