Skip to content

explicit_counter_loop produces incorrect advice #4677

Closed
@ChrisJefferson

Description

@ChrisJefferson

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingC-enhancementCategory: Enhancement of lints, like adding more cases or adding help messages

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions