Skip to content

shadow_unrelated raised when assigning couples #14377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
t-webber opened this issue Mar 8, 2025 · 3 comments · Fixed by #14381
Closed

shadow_unrelated raised when assigning couples #14377

t-webber opened this issue Mar 8, 2025 · 3 comments · Fixed by #14381
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@t-webber
Copy link

t-webber commented Mar 8, 2025

Summary

shadow_unrelated is fired when I assign a couple of variables, even if they are not shadowing anything; it is even fired if the variables were not assigned

Lint Name

shadow_unrelated

Reproducer

I tried this code:

#![allow(unused)]
#![warn(clippy::shadow_unrelated)]

fn main() {
    let a;
    let b;
    (a, b) = (0, 1);
}

I saw this happen:

warning: `b` shadows a previous, unrelated binding
 --> src/main.rs:7:9
  |
7 |     (a, b) = (0, 1);
  |         ^
  |
note: previous binding is here
 --> src/main.rs:7:6
  |
7 |     (a, b) = (0, 1);
  |      ^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#shadow_unrelated
note: the lint level is defined here
 --> src/main.rs:2:9
  |
2 | #![warn(clippy::shadow_unrelated)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^

I expected to see this happen: nothing

Version

rustc 1.87.0-nightly (f5a1ef712 2025-03-07)
binary: rustc
commit-hash: f5a1ef7121ad661b5a21a1d02941c8064d54ee0b
commit-date: 2025-03-07
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.0

Additional Labels

No response

@t-webber t-webber added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Mar 8, 2025
@t-webber t-webber changed the title shadow_unrelated raises when assigning couples shadow_unrelated raised when assigning couples Mar 8, 2025
@samueltardieu
Copy link
Contributor

Fun problem, which is due to the way tuple assignment is transformed by the compiler:

fn main() { let a; let b; { let (lhs, lhs) = (0, 1); a = lhs; b = lhs; }; }

@rustbot claim

@profetia
Copy link
Contributor

I wonder if this AssignDesugar behavior is newly introduced in the compiler. I also encountered one in #14371. Seems that it was not considered in clippy in the past.

@samueltardieu
Copy link
Contributor

I wonder if this AssignDesugar behavior is newly introduced in the compiler. I also encountered one in #14371. Seems that it was not considered in clippy in the past.

It has been introduced in 2020, but even the compiler documentation was incomplete until last September.

github-merge-queue bot pushed a commit that referenced this issue Mar 27, 2025
When lowering a destructuring assignment from AST to HIR, the compiler
will reuse the same identifier name (namely `sym::lhs`) for all the
fields. The desugaring must be checked for to avoid a false positive of
the `shadow_unrelated` lint.

Fix #10279
Fix #14377

changelog: [`shadow_unrelated`]: prevent false positive in destructuring
assignments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants