Skip to content

incorrect needless_range_loop #17432

Description

@yshui

Summary

needless_range_loop incorrectly complains about multi-level arrays.

Lint Name

needless_range_loop

Reproducer

I tried this code:

fn fun(a: [[usize; 5]; 5]) {
    let mut sum = 0;
    for i in 0..5 {
        sum += a[sum % 5][i];
    }
}

I saw this happen:

warning: the loop variable `i` is only used to index `a`
 --> src/main.rs:4:14
  |
4 |     for i in 0..5 {
  |              ^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_range_loop
  = note: `#[warn(clippy::needless_range_loop)]` on by default
help: consider using an iterator
  |
4 -     for i in 0..5 {
4 +     for <item> in &a {
  |

I expected to see this happen:

no warning

Version


Additional Labels

No response

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions