Skip to content

Commit

Permalink
Auto merge of #4803 - tomprogrammer:issue-4732, r=phansch
Browse files Browse the repository at this point in the history
Fix false positive in explicit_counter_loop lint

When the counter was used in a closure after the loop the lint didn't detect the
usage of the counter correctly.

Fix false positive in `explicit_counter_loop`

Fixes #4732
  • Loading branch information
bors committed Nov 11, 2019
2 parents 86b8643 + c88afce commit 609bc74
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion clippy_lints/src/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2194,8 +2194,9 @@ impl<'a, 'tcx> Visitor<'tcx> for InitializeVisitor<'a, 'tcx> {
}
walk_expr(self, expr);
}

fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
NestedVisitorMap::None
NestedVisitorMap::OnlyBodies(&self.cx.tcx.hir())
}
}

Expand Down
13 changes: 13 additions & 0 deletions tests/ui/explicit_counter_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,16 @@ mod issue_1670 {
}
}
}

mod issue_4732 {
pub fn test() {
let slice = &[1, 2, 3];
let mut index = 0;

// should not trigger the lint because the count is used after the loop
for _v in slice {
index += 1
}
let _closure = || println!("index: {}", index);
}
}

0 comments on commit 609bc74

Please sign in to comment.