rust-lang / rust-lang/rust-clippy

needless_range_loop makes code longer and less understandable

Open
#10,643 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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 makes code longer and less understandable

Lint Name

needless_range_loop

Reproducer

I tried this code:

    for i in 0..mem.len() { mem[i] = i; }

I saw this happen:

warning: the loop variable `i` is used to index `mem`
   --> /src/lib.rs:671:14
    |
671 |     for i in 0..mem.len() { mem[i] = i; }
    |              ^^^^^^^^^^^^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_range_loop
help: consider using an iterator
    |
671 |     for (i, <item>) in mem.iter_mut().enumerate() { mem[i] = i; }
    |         ~~~~~~~~~~~    ~~~~~~~~~~~~~~~~~~~~~~~~~~

I expected to see this happen: no lints here.

Version
rustc 1.68.1 (8460ca823 2023-03-20)
binary: rustc
commit-hash: 8460ca823e8367a30dda430efda790588b8c84d3
commit-date: 2023-03-20
host: x86_64-apple-darwin
release: 1.68.1
LLVM version: 15.0.6
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.

Research direction

Start by locating the needless_range_loop lint implementation and its existing tests, then run the Rust reproducer from the issue. Check why the index assignment triggers the lint and whether the suggested iterator form is applicable; done means the reproducer no longer emits this lint while other valid cases remain covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.