Skip to content
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

shadow_unrelated incorrectly flags a destructuring assignment #10279

Open
Kromey opened this issue Feb 2, 2023 · 4 comments
Open

shadow_unrelated incorrectly flags a destructuring assignment #10279

Kromey opened this issue Feb 2, 2023 · 4 comments
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

@Kromey
Copy link

Kromey commented Feb 2, 2023

Summary

Clippy appears to incorrectly flag the second variable in a destructuring assignment as shadowing the first, even when the two variables are different.

I do not see this behavior with a destructuring let binding, even when they do shadow other variables.

Lint Name

shadow_unrelated

Reproducer

I tried this code:

#![warn(clippy::shadow_unrelated)]

fn recompute(a: u32, b: u32) -> (u32, u32) {
    (a * 2, b * 4)
}

fn main() {
    let (mut start, mut end) = (12, 15);
    println!("{start}, {end}");
    (start, end) = recompute(start, end); // Lint flags this line
    println!("{start}, {end}");
}

I saw this happen:

warning: `end` shadows a previous, unrelated binding
  --> src/main.rs:10:13
   |
10 |     (start, end) = recompute(start, end);
   |             ^^^
   |
note: previous binding is here
  --> src/main.rs:10:6
   |
10 |     (start, end) = recompute(start, end);
   |      ^^^^^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#shadow_unrelated

I expected to see this happen:
No warnings should appear

Additional note:
If I change the flagged line to let (start, end) = ... (and remove the now-unnecessary muts), clippy is happy even though both start and end are indeed being shadowed (albeit not unrelated). So the issue seems to be related specifically to this destructuring assignment.

Version

rustc 1.67.0 (fc594f156 2023-01-24)
binary: rustc
commit-hash: fc594f15669680fa70d255faec3ca3fb507c3405
commit-date: 2023-01-24
host: x86_64-unknown-linux-gnu
release: 1.67.0
LLVM version: 15.0.6

Additional Labels

No response

@Kromey Kromey 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 Feb 2, 2023
@Rudxain
Copy link
Contributor

Rudxain commented Mar 7, 2023

I have the same problem with this

const fn gcd(mut a: i128, mut b: i128) -> i128 {
	while b != 0 {
		(a, b) = (b, a % b);
	}
	a
}

I find it weird that something so simple doesn't seem to be covered by tests (I haven't read the tests in this repo, so don't take me seriously)

@matte1
Copy link

matte1 commented Jan 13, 2024

I hit this as well!

@QTi1
Copy link

QTi1 commented Dec 12, 2024

Here's another example without anything mutable:

#![warn(clippy::shadow_unrelated)]
fn main() {
    let (zero, one);
    (zero, one) = (0, 1); // Lint flags this line
    println!("{zero}, {one}");
}

@GitPinkRabbit
Copy link

+1, hope to fix. Considering #13795 is fixed.

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

No branches or pull requests

5 participants