Lint dangerous uses of shadowing · Issue #3433 · rust-lang/rust-clippy (original) (raw)

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

#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