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 false positive with question mark #6563

Closed
camsteffen opened this issue Jan 7, 2021 · 3 comments · Fixed by #7338
Closed

shadow_unrelated false positive with question mark #6563

camsteffen opened this issue Jan 7, 2021 · 3 comments · Fixed by #7338
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

@camsteffen
Copy link
Contributor

Lint name: shadow_unrelated

I tried this code:

#[warn(clippy::shadow_unrelated)]
fn foo() -> Option<i32> {
    let a = 1_i32;
    let a = Some(1).map(|_| a)?;
    Some(a)
}

I expected to see this happen: no lint

Instead, this happened: shadow_unrelated lint

I needed a closure and a question mark to reproduce this. 🤷

@camsteffen camsteffen 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 Jan 7, 2021
@dpathakj
Copy link

This might be the same issue as #6141.

@whatisaphone
Copy link
Contributor

On the playground it repros on stable and nightly with just a closure.

Playground link

#![deny(clippy::shadow_unrelated)]

fn main() {
    let foo = 0;
    let foo = || foo;
}

Expected result: no error, since || foo refers to the variable foo

Actual result: shadow_unrelated error
error: `foo` is being shadowed
 --> src/main.rs:5:9
  |
5 |     let foo = || foo;
  |         ^^^
  |
note: the lint level is defined here
 --> src/main.rs:1:9
  |
1 | #![deny(clippy::shadow_unrelated)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^
note: initialization happens here
 --> src/main.rs:5:15
  |
5 |     let foo = || foo;
  |               ^^^^^^
note: previous binding is here
 --> src/main.rs:4:9
  |
4 |     let foo = 0;
  |         ^^^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#shadow_unrelated

@lopopolo
Copy link

lopopolo commented Sep 4, 2021

This is another bizarre false positive I get:

warning: `self.state.as_deref_mut().ok_or_else(InterpreterExtractError::new)?` is being shadowed
  --> artichoke-backend/src/parser.rs:32:21
   |
32 |         let state = self.state.as_deref_mut().ok_or_else(InterpreterExtractError::new)?;
   |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: the lint level is defined here
  --> artichoke-backend/src/lib.rs:2:9
   |
2  | #![warn(clippy::pedantic)]
   |         ^^^^^^^^^^^^^^^^
   = note: `#[warn(clippy::shadow_unrelated)]` implied by `#[warn(clippy::pedantic)]`
note: initialization happens here
  --> artichoke-backend/src/parser.rs:32:21
   |
32 |         let state = self.state.as_deref_mut().ok_or_else(InterpreterExtractError::new)?;
   |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: previous binding is here
  --> artichoke-backend/src/parser.rs:31:36
   |
31 |     fn add_fetch_lineno(&mut self, val: usize) -> Result<usize, Self::Error> {
   |                                    ^^^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#shadow_unrelated

bors added a commit that referenced this issue Sep 30, 2021
Re-write shadow lints

changelog: Move shadow_unrelated to restriction
changelog: The shadow lints find a lot more shadows and are not limited to certain patterns

Drastically simplifies the implementation. Catches a lot more cases.

I removed the "initialization happens here" note. It is not helpful IMO.

Closes #318
Fixes #2890
Fixes #6563
Fixes #7588
Fixes #7620
@bors bors closed this as completed in f8303ad Sep 30, 2021
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.

4 participants