rust-lang / rust-lang/rust-clippy
`shadow_unrelated` closure acceptance is defeated by wrapping the closure in a pointer
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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