From 1b67aa74bdcbf3939241e19dd16ff0bcc6150d94 Mon Sep 17 00:00:00 2001 From: Elliot Bobrow Date: Fri, 24 Dec 2021 13:20:40 -0800 Subject: [PATCH] fix `shadow_reuse` false negative for if let bindings --- clippy_lints/src/shadow.rs | 4 ++-- tests/ui/shadow.rs | 2 ++ tests/ui/shadow.stderr | 18 +++++++++++++++--- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/clippy_lints/src/shadow.rs b/clippy_lints/src/shadow.rs index f6880af0cab..516cb939c15 100644 --- a/clippy_lints/src/shadow.rs +++ b/clippy_lints/src/shadow.rs @@ -220,14 +220,14 @@ fn is_self_shadow(cx: &LateContext<'_>, pat: &Pat<'_>, mut expr: &Expr<'_>, hir_ } } -/// Finds the "init" expression for a pattern: `let = ;` or +/// Finds the "init" expression for a pattern: `let = ;` (or `if let`) or /// `match { .., => .., .. }` fn find_init<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Option<&'tcx Expr<'tcx>> { for (_, node) in cx.tcx.hir().parent_iter(hir_id) { let init = match node { Node::Arm(_) | Node::Pat(_) => continue, Node::Expr(expr) => match expr.kind { - ExprKind::Match(e, _, _) => Some(e), + ExprKind::Match(e, _, _) | ExprKind::Let(_, e, _) => Some(e), _ => None, }, Node::Local(local) => local.init, diff --git a/tests/ui/shadow.rs b/tests/ui/shadow.rs index 06f6949b66f..0321f8c4cdf 100644 --- a/tests/ui/shadow.rs +++ b/tests/ui/shadow.rs @@ -47,6 +47,8 @@ fn syntax() { let _ = |[x]: [u32; 1]| { let x = 1; }; + let y = Some(1); + if let Some(y) = y {} } fn negative() { diff --git a/tests/ui/shadow.stderr b/tests/ui/shadow.stderr index dcc7d4e6b2f..f8b9221d555 100644 --- a/tests/ui/shadow.stderr +++ b/tests/ui/shadow.stderr @@ -241,17 +241,29 @@ note: previous binding is here 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 - --> $DIR/shadow.rs:85:9 + --> $DIR/shadow.rs:87:9 | LL | let _b = _a; | ^^ | note: previous binding is here - --> $DIR/shadow.rs:84:28 + --> $DIR/shadow.rs:86:28 | LL | pub async fn foo2(_a: i32, _b: i64) { | ^^ -error: aborting due to 21 previous errors +error: aborting due to 22 previous errors