rust-lang / rust-lang/rust-clippy
False positive: needless_range_loop
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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