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 is using idents from RHS of assignment #6141

Open
lopopolo opened this issue Oct 8, 2020 · 3 comments
Open

shadow_unrelated is using idents from RHS of assignment #6141

lopopolo opened this issue Oct 8, 2020 · 3 comments
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@lopopolo
Copy link

lopopolo commented Oct 8, 2020

Clippy outputs these warnings:

warning: `state
            .parser
            .as_mut()
            .ok_or(InterpreterExtractError::new())?` is being shadowed
  --> artichoke-backend/src/parser.rs:39:22
   |
39 |           let parser = state
   |  ______________________^
40 | |             .parser
41 | |             .as_mut()
42 | |             .ok_or(InterpreterExtractError::new())?;
   | |___________________________________________________^
   |
note: initialization happens here
  --> artichoke-backend/src/parser.rs:39:22
   |
39 |           let parser = state
   |  ______________________^
40 | |             .parser
41 | |             .as_mut()
42 | |             .ok_or(InterpreterExtractError::new())?;
   | |___________________________________________________^
note: previous binding is here
  --> artichoke-backend/src/parser.rs:38:21
   |
38 |         let state = self.state.as_mut().ok_or(InterpreterExtractError::new())?;
   |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#shadow_unrelated

warning: `u16::try_from(new)
            .map_err(|_| IncrementLinenoError::Overflow(usize::from(u16::MAX)))?` is being shadowed
  --> artichoke-backend/src/state/parser.rs:68:21
   |
68 |           let store = u16::try_from(new)
   |  _____________________^
69 | |             .map_err(|_| IncrementLinenoError::Overflow(usize::from(u16::MAX)))?;
   | |________________________________________________________________________________^
   |
note: initialization happens here
  --> artichoke-backend/src/state/parser.rs:68:21
   |
68 |           let store = u16::try_from(new)
   |  _____________________^
69 | |             .map_err(|_| IncrementLinenoError::Overflow(usize::from(u16::MAX)))?;
   | |________________________________________________________________________________^
note: previous binding is here
  --> artichoke-backend/src/state/parser.rs:65:19
   |
65 |           let new = old
   |  ___________________^
66 | |             .checked_add(val)
67 | |             .ok_or_else(|| IncrementLinenoError::Overflow(usize::from(u16::MAX)))?;
   | |__________________________________________________________________________________^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#shadow_unrelated

For this code:

    fn add_fetch_lineno(&mut self, val: usize) -> Result<usize, Self::Error> {
        let state = self.state.as_mut().ok_or(InterpreterExtractError::new())?;
        let parser = state
            .parser
            .as_mut()
            .ok_or(InterpreterExtractError::new())?;
        let lineno = parser.add_fetch_lineno(val)?;
        Ok(lineno)
    }

and

    pub fn add_fetch_lineno(&mut self, val: usize) -> Result<usize, IncrementLinenoError> {
        let old = usize::from(unsafe { self.context.as_ref() }.lineno);
        let new = old
            .checked_add(val)
            .ok_or_else(|| IncrementLinenoError::Overflow(usize::from(u16::MAX)))?;
        let store = u16::try_from(new)
            .map_err(|_| IncrementLinenoError::Overflow(usize::from(u16::MAX)))?;
        unsafe {
            self.context.as_mut().lineno = store;
        }
        Ok(new)
    }

I expected to see this happen: No errors, as in Rust 1.46.0

Meta

  • cargo clippy -V: clippy 0.0.212 (18bf6b4f0 2020-10-07)
  • rustc -Vv:
    rustc 1.47.0 (18bf6b4f0 2020-10-07)
    binary: rustc
    commit-hash: 18bf6b4f01a6feaf7259ba7cdae58031af1b7b39
    commit-date: 2020-10-07
    host: x86_64-apple-darwin
    release: 1.47.0
    LLVM version: 11.0
    
@lopopolo lopopolo added the C-bug Category: Clippy is not doing the correct thing label Oct 8, 2020
@lopopolo lopopolo changed the title shadow_unrelated is using idents from RHS of asignment shadow_unrelated is using idents from RHS of assignment Oct 8, 2020
lopopolo added a commit to artichoke/artichoke that referenced this issue Oct 8, 2020
Also address new clippy lints and updates.

This commit disables some lints due to rust-lang/rust-clippy#6137 and
rust-lang/rust-clippy#6141.
@dpathakj
Copy link

dpathakj commented Jan 12, 2021

Here's a minimal example of what I think might be the same thing, using the recently landed #![feature(destructuring_assignment)]:

#[warn(clippy::shadow_unrelated)]
fn multiple_assignment_produces_false_positive() {
    let mut n = 0;
    let mut b = false;
    loop {
        (n, b) = f(n, b); // warning: `b` is being shadowed - doesn't warn about `n`, though
    }
}

fn f(a: usize, b: bool) -> (usize, bool) {
    (a + 1, !b)
}

With a single assignment, there's no warning:

#[warn(clippy::shadow_unrelated)]
fn single_assignment_produces_true_negative() {
    let mut n = 0;
    loop {
        n = f_prime(n); // no warning about `n` being shadowed
    }
}

fn f_prime(a: usize) -> usize {
    a + 1
}

@atlv24
Copy link

atlv24 commented Nov 7, 2022

Would this be the same issue as

#[warn(clippy::shadow_unrelated)]
pub fn example(v: &mut u32) -> u32 {
    let s;
    (*v, s) = (0, 0);
    //   ^ `s` shadows a previous, unrelated binding
    s
}

Or should I open a new issue? Key difference is the second var is an initialization not a mutation.

@llogiq
Copy link
Contributor

llogiq commented Nov 7, 2022

Clippy doesn't see a difference between delayed init and mutation, so I don't think a new issue is necessary.

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
Projects
None yet
Development

No branches or pull requests

4 participants