rust-lang / rust-lang/rust-clippy

FP mut_range_bound: range bound modified inside enclosing loop

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

Nobody has claimed this yet.

C-bug E-hard I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

In this code we first create the mut range bound, and mutate it in another loop which means the loop that actually uses the range is executed several times with different bounds (even if it is true that inside the usage of the range, changing the index has no effect on the inner loop.

fn main() {
    let mut i = 3;
    while i < 1000 { // this invalidates the lint warning
        for x in i..9 { // this will be unaffected by the i+=4 but we modify i AND LOOP AGAIN!
            i += 4; // lint warns about this
        }

        if i > 12 {
            return;
        }
    }
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=30343889f1b8be663adb15c2d8184c2a

The lint should probably only fire if range bound variable and the range bounds usage are in the same scope?

Meta

clippy 0.0.212 (1fd5b9d 2020-09-20)

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 reproducing the example and tracing the mut_range_bound lint implementation. Compare the lint’s behavior when the range is reused by an enclosing loop, and verify that the completed change no longer warns for this case while preserving the warning for the intended same-scope mutation.

Written by the indexing model from the issue text.

Assessment

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