Auto merge of #8165 - ebobrow:shadow_reuse_fn, r=xFrednet

fix [`shadow_reuse`] false negative for if let bindings

fixes #8087

changelog: trigger [`shadow_reuse`] instead of [`shadow_unrelated`] on shadowed `if let` bindings
This commit is contained in:
bors 2021-12-25 11:58:25 +00:00
commit eb24acf60d
3 changed files with 19 additions and 5 deletions

View File

@ -220,14 +220,14 @@ fn is_self_shadow(cx: &LateContext<'_>, pat: &Pat<'_>, mut expr: &Expr<'_>, hir_
} }
} }
/// Finds the "init" expression for a pattern: `let <pat> = <init>;` or /// Finds the "init" expression for a pattern: `let <pat> = <init>;` (or `if let`) or
/// `match <init> { .., <pat> => .., .. }` /// `match <init> { .., <pat> => .., .. }`
fn find_init<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Option<&'tcx Expr<'tcx>> { fn find_init<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Option<&'tcx Expr<'tcx>> {
for (_, node) in cx.tcx.hir().parent_iter(hir_id) { for (_, node) in cx.tcx.hir().parent_iter(hir_id) {
let init = match node { let init = match node {
Node::Arm(_) | Node::Pat(_) => continue, Node::Arm(_) | Node::Pat(_) => continue,
Node::Expr(expr) => match expr.kind { Node::Expr(expr) => match expr.kind {
ExprKind::Match(e, _, _) => Some(e), ExprKind::Match(e, _, _) | ExprKind::Let(_, e, _) => Some(e),
_ => None, _ => None,
}, },
Node::Local(local) => local.init, Node::Local(local) => local.init,

View File

@ -47,6 +47,8 @@ fn syntax() {
let _ = |[x]: [u32; 1]| { let _ = |[x]: [u32; 1]| {
let x = 1; let x = 1;
}; };
let y = Some(1);
if let Some(y) = y {}
} }
fn negative() { fn negative() {

View File

@ -241,17 +241,29 @@ note: previous binding is here
LL | let _ = |[x]: [u32; 1]| { LL | let _ = |[x]: [u32; 1]| {
| ^ | ^
error: `y` is shadowed
--> $DIR/shadow.rs:51:17
|
LL | if let Some(y) = y {}
| ^
|
note: previous binding is here
--> $DIR/shadow.rs:50:9
|
LL | let y = Some(1);
| ^
error: `_b` shadows a previous, unrelated binding error: `_b` shadows a previous, unrelated binding
--> $DIR/shadow.rs:85:9 --> $DIR/shadow.rs:87:9
| |
LL | let _b = _a; LL | let _b = _a;
| ^^ | ^^
| |
note: previous binding is here note: previous binding is here
--> $DIR/shadow.rs:84:28 --> $DIR/shadow.rs:86:28
| |
LL | pub async fn foo2(_a: i32, _b: i64) { LL | pub async fn foo2(_a: i32, _b: i64) {
| ^^ | ^^
error: aborting due to 21 previous errors error: aborting due to 22 previous errors