rust-lang / rust-lang/rust-clippy

`shadow_unrelated` closure acceptance is defeated by wrapping the closure in a pointer

Open
#14,537 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

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

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 with the shadow_unrelated lint entry point and run the reproducer from the issue. Compare the direct, borrowed, mutable-borrowed, and Box::new-wrapped closure cases. Done means the pointer-wrapped closure cases match the direct closure case without producing the unrelated-shadowing lint.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.