rust-lang / rust-lang/rust-clippy

shadow_unrelated is using idents from RHS of assignment

Open
#6,141 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

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
    

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

Start by reproducing the shadow_unrelated warnings with the two Rust snippets and compare the behavior between Rust 1.46.0 and 1.47.0. Trace the lint's handling of assignment right-hand sides, then verify that the shown state/parser and old/new/store examples no longer produce false-positive warnings while unrelated shadowing remains diagnosed.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.