Skip to content

loop counter false positive #384

Closed
Closed
@frewsxcv

Description

@frewsxcv
#![feature(plugin)]

#![plugin(clippy)]


fn main(){
    let mut i = 0;

    for _ in 1..10 {
        i += 1;
    }

    println!("{}", i);
}

results in

src/lib.rs:9:5: 11:6 warning: the variable `i` is used as a loop counter. Consider using `for (i, item) in 1..10.enumerate()` or similar iterators, #[warn(explicit_counter_loop)] on by default
src/lib.rs: 9     for _ in 1..10 {
src/lib.rs:10         i += 1;
src/lib.rs:11     }
src/lib.rs:9:5: 11:6 note: in this expansion of for loop expansion
src/lib.rs:9:5: 11:6 help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#explicit_counter_loop

From what I understand, it is not possible to replicate the behavior of my program using enumerate, so it shouldn't be suggested if the loop counter value is used outside the loop body

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.T-middleType: Probably requires verifiying types

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions