rust-lang / rust-lang/rust-clippy
New Lint for mutable for-loop counter variable
Open
Nobody has claimed this yet.
A-lint
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
Given a program like so:
fn main() {
for mut i in 0..10 {
i = 10;
println!("{}",i);
}
}
Naively, the lint would warn on i being mutable and or suggest users do the following:
fn main() {
for i in 0..10 {
let k = 10;
println!("{}",k);
}
}
Advantage
- The original code gives the impression that the loop will only run once, whereas it will run 10 times.
- The new code does not use the variable
iand so it is clear that the code will run 10 times.
Drawbacks
No response
Example
fn main() {
for mut i in 0..10 {
i = 10;
println!("{}",i);
}
}
Could be written as:
fn main() {
for i in 0..10 {
let k = 10;
println!("{}",k);
}
}
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 by reviewing the Rust examples in the issue and how the proposed lint should distinguish a mutable loop counter from an ordinary mutable binding. Done means the lint identifies the shown pattern and provides an appropriate diagnostic or suggestion without changing valid loop behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100