rust-lang / rust-lang/rust-clippy

shadow_unrelated breaks when intermediary bindings are used

Open
#3,200 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Playground:

pub fn foo(a: i32) {
    let b = 3 * (3 + 1) / 2;
    let c = (f64::from(a) / f64::from(b)) as i32;

    let a = c + b;

    let _ = a;
}

produces

error: `a` is shadowed by `c + b`
 --> src/main.rs:8:9
  |
8 |     let a = c + b;
  |         ^
  |
note: lint level defined here
 --> src/main.rs:2:44
  |
2 | #![cfg_attr(feature = "cargo-clippy", deny(clippy::shadow_unrelated))]
  |                                            ^^^^^^^^^^^^^^^^^^^^^^^^
note: initialization happens here
 --> src/main.rs:8:13
  |
8 |     let a = c + b;
  |             ^^^^^
note: previous binding is here
 --> src/main.rs:4:12
  |
4 | pub fn foo(a: i32) {

The lint incorrectly errors that the new binding a is unrelated to the function argument a, but this is incorrect since these two bindings are related via c.

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

Reproduce the example in the linked Rust Playground and begin at the shadow_unrelated lint entry point. Trace how bindings connected through c are classified, then verify that the shown a binding is no longer reported as unrelated.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.