rust-lang / rust-lang/rust-clippy

incorrect `needless_range_loop`

Open
#17,432 1 comment 0 reactions 1 assignee View on GitHub

@XhesicaFrost is already working on this.

Since Jul 20, 2026.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.