rust-lang / rust-lang/rust-clippy
Suggestion from needless_range_loop removes too many array index operations
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
Clippy's suggestion for a needles_range_loop lint where the loop body indexes doubly-nested Vecs removes both sets of square brackets, instead of only the last one.
Reproducer
Playground link: https://play.rust-lang.org/?version=beta&mode=debug&edition=2024&gist=7b400efc4213e580381fa0fccf93459a
I tried this code:
use std::iter::repeat_n;
fn main() {
let mut wire_polys = repeat_n(vec!['x'; 7], 10).collect::<Vec<_>>();
let mut inp = repeat_n(String::new(), 10).collect::<Vec<_>>();
for i in 0..10 {
for j in 0..7 {
wire_polys[i][j] = 'y';
}
inp[i] = format!("{i} {:?}", wire_polys[i]);
}
}
This is minimized from https://github.com/divviup/libprio-rs/blob/574c75ecda60323e087f29e221862b1713459d6c/src/flp/gadgets.rs#L563-L568
I got the following suggestion:
warning: the loop variable `j` is only used to index `wire_polys`
--> src/main.rs:7:18
|
7 | for j in 0..7 {
| ^^^^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/beta/index.html#needless_range_loop
= note: `#[warn(clippy::needless_range_loop)]` on by default
help: consider using an iterator
|
7 - for j in 0..7 {
7 + for <item> in wire_polys.iter_mut().take(7) {
|
The for loop should instead iterate over wire_polys[i].iter_mut().take(7).
Version
rustc 1.91.0 (f8297e351 2025-10-28)
binary: rustc
commit-hash: f8297e351a40c1439a467bbbb6879088047f50b3
commit-date: 2025-10-28
host: x86_64-unknown-linux-gnu
release: 1.91.0
LLVM version: 21.1.2
Additional Labels
@rustbot label +I-suggestion-causes-error
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 nested-Vec case using the linked Playground example. Compare the emitted suggestion with the requested wire_polys[i].iter_mut().take(7) form, then add coverage for the nested indexing case and verify that the suggestion no longer removes both index operations.
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
- 48/100