rust-lang / rust-lang/rust-clippy

False positive: needless_range_loop

Open
#11,399 2 comments 1 reaction 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

Related code:

for i in 0..message.len() {
        block.data[i] = message[i] as char;
 }

Clippy begs me to use rust for x in data
without the 'i' enumerator, however this would make fetching message[i] impossible.

Lint Name

needless_range_loop

Reproducer

I tried this code:

for i in 0..message.len() {
        block.data[i] = message[i] as char;
}

I saw this happen:

the loop variable `i` is used to index `message` Help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_range_loop Help: consider using an iterator and enumerate()

I expected to see this happen:

no complaints from Clippy

Version
rustc 1.72.0-nightly (8c74a5d27 2023-06-14)
binary: rustc
commit-hash: 8c74a5d27c644a0f7a22bb2fa8dd3ff8257bc220
commit-date: 2023-06-14
host: x86_64-pc-windows-msvc
release: 1.72.0-nightly
LLVM version: 16.0.5
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 with the needless_range_loop lint and reproduce the reported Rust example to understand why it is flagged. Trace the lint's handling of indexed loops, then add or update a focused regression test showing the expected behavior; done means the valid use of message[i] no longer produces this false positive.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.