rust-lang / rust-lang/rust-clippy

New Lint for mutable for-loop counter variable

Open
#11,033 0 comments 0 reactions 0 assignees View on GitHub

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 i and 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

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.