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

explicit_counter_loop produces incorrect advice #4677

Closed
ChrisJefferson opened this issue Oct 16, 2019 · 0 comments · Fixed by #6076
Closed

explicit_counter_loop produces incorrect advice #4677

ChrisJefferson opened this issue Oct 16, 2019 · 0 comments · Fixed by #6076
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

Comments

@ChrisJefferson
Copy link

Using clippy 0.0.212 (3aea860 2019-09-03), consider the following function (which was reduced from a larger function

pub fn loopy(x : Vec<isize>) {
    let mut l = 0;
    for i in x {
        println!("1:{}:{}\n",i,l);
        l += 1;
        println!("1:{}:{}\n",i,l);
    }
}

Clippy suggests:

warning: the variable `l` is used as a loop counter.
   --> src/game.rs:350:14
    |
350 |     for i in x {
    |              ^ help: consider using: `for (l, i) in x.enumerate()`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#explicit_counter_loop

However, it isn't that simple, as l is used both before, and after incrementing. This might be OK, because just doing the suggested fix isn't enough anyway, so someone should hopefully notice what's going on. The other option would be to only suggest this if there is no further usage of the loop variable after it is incremented.

@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 16, 2019
@bors bors closed this as completed in 019c0d5 Sep 24, 2020
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants