Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive explicit_counter_loop when the counter is used in a closure after the loop #4732

Closed
madadam opened this issue Oct 25, 2019 · 1 comment · Fixed by #4803
Closed
Labels
C-bug Category: Clippy is not doing the correct thing C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy

Comments

@madadam
Copy link

madadam commented Oct 25, 2019

The explicit_counter_loop lint is incorrectly triggered in this case:

let vec = vec![1,2,3];
let mut index = 0;

for _v in &vec { index += 1 }

run(|| {
    println!("index: {}", index);
})

This is incorrect because the counter is used after the loop. Note that if the counter were used directly and not in a closure, the lint would not get triggered.

Rust playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=3f1f1216aa7a4344d6891fc9e4ccdd5c

> cargo clippy -V
clippy 0.0.212 (3aea860 2019-09-03)
@madadam madadam changed the title False positive explicit_counter_loop when the counter is used in a closuse after the loop False positive explicit_counter_loop when the counter is used in a closure after the loop Oct 25, 2019
@flip1995 flip1995 added C-bug Category: Clippy is not doing the correct thing C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages labels Oct 25, 2019
@flip1995
Copy link
Member

This should be fixable by changing this line:

NestedVisitorMap::None

to NestedVisitorMap::OnlyBodies(&self.cx.tcx.hir())

@flip1995 flip1995 added the good-first-issue These issues are a good way to get started with Clippy label Oct 25, 2019
bors added a commit that referenced this issue Nov 11, 2019
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.

Fixes #4732
bors added a commit that referenced this issue Nov 11, 2019
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
bors added a commit that referenced this issue Nov 11, 2019
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
@bors bors closed this as completed in 2646b10 Nov 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants