rust-lang / rust-lang/rust-clippy

for loop with mutable binding

Open
#6,246 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint S-needs-discussion
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Suggest replacing

for i in 0..11 {
    let mut i = i;

    i += 1;
    println!("i: {}", i);
}

with

for mut i in 0..11 {
    i += 1;
    println!("i: {}", i);
}

(I did not even know that this was possible, until I saw this post on reddit)

Categories (optional)
  • Kind: clippy::style

What is the advantage of the recommended code over the original code

less code -> easier to read

Drawbacks

None.

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 issue's two Rust examples and the existing clippy::style lint implementations and tests. Determine how a lint can recognize the mutable binding form and suggest the direct for-loop binding; done means the simpler form is suggested without changing behavior, with coverage for the shown example.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
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.