Skip to content

Weird warning for needless_range_loop #398

Open
@antoyo

Description

@antoyo

Hello.

I found a weird warning for a range loop.

In the following code:

#![feature(plugin)]
#![plugin(clippy)]

fn main() {
    let string = "test";
    let vec = vec![0, 1, 2, 3];
    for i in 0 .. string.len() {
        println!("{}", i);
        println!("{}", vec[i]);
    }
}

I get the following warning:

src/main.rs:7:5: 10:6 warning: the loop variable `i` is used to index `vec`. Consider using `for (i, item) in vec.iter().enumerate()` or similar iterators, #[warn(needless_range_loop)] on by default
src/main.rs: 7     for i in 0 .. string.len() {
src/main.rs: 8         println!("{}", i);
src/main.rs: 9         println!("{}", vec[i]);
src/main.rs:10     }
src/main.rs:7:5: 10:6 help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#needless_range_loop

This is wrong since I am iterating over the string, not the vec.

If I remove the line using the vec, the warning is gone.

Thanks to fix this issue.

(By the way, is there an option to span errors instead of warnings in clippy?)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions