rust-lang / rust-lang/rust-clippy

Lint dangerous uses of shadowing

Open
#3,433 10 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint E-medium L-guidelines
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

We have three shadowing lints

which all try to address shadowing issues. Unfortunately enabling either of these lints will also lint idiomatic and readable code. We should find a way to write a lint that only detects actually problematic cases of shadowing.

All of the cases below are even more prominent if you include mutable bindings, as it becomes very hard to distinguish which variable is changed by a x = ... statement.

Shadowing in a small scope

let x = foo;
....
{
    let x = bar;
    ...
    use(x);
    use1(x);
}
use2(x); // at this point we see the `use1(x)` above and
// may assume these two `x` are related

Shadowing in match arm patterns

https://github.com/rust-lang-nursery/rust-clippy/issues/2890

let x = foo;
...
match bar {
    Some(x) => use(x),
    None => use(x),
}

Shadowing in conditional early aborts

probably not as problematic as the first case mentioned, but would get caught by a naive implementation of the first case.

let x = foo;
....
if meh {
    let x = bar;
    ...
    use(x);
    use1(x);
    return;
}
use2(x); // at this point we see the `use1(x)` above and
// may assume these two `x` are related

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 reviewing the existing shadow_reuse, shadow_same, and shadow_unrelated lints and the linked issue about shadowing in match arm patterns. Compare the listed scope, pattern, and early-abort examples with the cases those lints currently report. Done means defining and implementing a lint that catches genuinely problematic shadowing without flagging idiomatic, readable code.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.