Skip to content

shadow_unrelated closure acceptance is defeated by wrapping the closure in a pointer #14537

Open
@kpreid

Description

@kpreid

Summary

#13677 made shadow_unrelated recognize that a parameter to a closure passed to a function is probably related, but a Box::new() or any kind of pointer (as is needed for a dyn closure) defeats this.

Lint Name

shadow_unrelated

Reproducer

I tried this code:

#![deny(clippy::shadow_unrelated)]
#![allow(clippy::needless_borrows_for_generic_args)]

pub fn example() {
    let x = Some(1);
    
    // No lint
    // Example from <https://github.com/rust-lang/rust-clippy/pull/13677>
    let _y1 = x.map(|x| x + 1);
    
    // Lint
    let _y2 = x.map(&|x| x + 1);
    let _y3 = x.map(&mut |x| x + 1);
    let _y4 = x.map(Box::new(|x| x + 1));
}

I saw this happen:

error: `x` shadows a previous, unrelated binding
  --> src/lib.rs:12:23
   |
12 |     let _y2 = x.map(&|x| x + 1);
   |                       ^
   |
note: previous binding is here
  --> src/lib.rs:5:9
   |
5  |     let x = Some(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/lib.rs:1:9
   |
1  | #![deny(clippy::shadow_unrelated)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^

error: `x` shadows a previous, unrelated binding
  --> src/lib.rs:13:27
   |
13 |     let _y3 = x.map(&mut |x| x + 1);
   |                           ^
   |
note: previous binding is here
  --> src/lib.rs:5:9
   |
5  |     let x = Some(1);
   |         ^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#shadow_unrelated

error: `x` shadows a previous, unrelated binding
  --> src/lib.rs:14:31
   |
14 |     let _y4 = x.map(Box::new(|x| x + 1));
   |                               ^
   |
note: previous binding is here
  --> src/lib.rs:5:9
   |
5  |     let x = Some(1);
   |         ^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#shadow_unrelated

I expected to see this happen: No lint

Version

1.88.0-nightly (2025-04-02 d5b4c2e4f19b6d703737)

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions