rust-lang / rust-lang/rust-clippy

immutable loop condition lint gives up on mutated upvars

Open
#2,916 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Currently the immutable loop condition lint gives up on code like the following, where the variable being mutated is an upvar:

let (mut a, mut b) = (0, 0);

// this should be okay
// (and it is, but only because clippy gives up; it doesn't
//  know how to compare the 'a' in the body to the 'a' in the condition)
let mut f = || {
    while a < 5 {
        a += 1;
    }
};
f();

// this should produce an error (but clippy doesn't even try)
let mut f = || {
    while a < 5 {
        b += 1;
    }
};
f();

This is a known problem with a FIXME in the source, but I think that fixing it (or at least, discovering how to fix it) will be a useful step towards implementing #2914, which requires much more reliable ways of discovering whether a given variable is used.

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 tracing the immutable loop condition lint and its FIXME around comparing variables captured as closure upvars. Reproduce both Rust closure examples, then verify that mutating b while testing a produces a diagnostic while mutating a remains accepted; consider how this supports issue #2914.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.