Skip to content

False Positive: needless_range_loop #2277

@ghost

Description

This example code produces the clippy error below.

pub fn example(list: &[[f64; 3]]) {
    let mut x: [f64; 3] = [10.; 3];

    for i in 0..3 {
        x[i] = list
            .iter()
            .map(|item| item[i])
            .sum::<f64>();
    }
}
warning: the loop variable `i` is only used to index `x`.
 --> src/lib.rs:4:5
  |
4 | /     for i in 0..3 {
5 | |         x[i] = list
6 | |             .iter()
7 | |             .map(|item| item[i])
8 | |             .sum::<f64>();
9 | |     }
  | |_____^
  |
  = note: #[warn(needless_range_loop)] on by default
  = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.176/index.html#needless_range_loop
help: consider using an iterator
  |
4 |     for <item> in x.iter_mut().take(3) {
  |

The warning is incorrect. i is used in the closure within the map call. The suggestion won't work because of that.

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