diff options
| author | bors <bors@rust-lang.org> | 2021-12-25 11:58:25 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2021-12-25 11:58:25 +0000 |
| commit | eb24acf60d02ecc0d1f2ef8ff041f7b10429b545 (patch) | |
| tree | cd3267d67711f5c3049e03193cc19f1b5c80ac46 | |
| parent | 8529b2a0561dcee254ee1447aa6bd5d69812b697 (diff) | |
| parent | 1b67aa74bdcbf3939241e19dd16ff0bcc6150d94 (diff) | |
| download | rust-eb24acf60d02ecc0d1f2ef8ff041f7b10429b545.tar.gz rust-eb24acf60d02ecc0d1f2ef8ff041f7b10429b545.zip | |
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
| -rw-r--r-- | clippy_lints/src/shadow.rs | 4 | ||||
| -rw-r--r-- | tests/ui/shadow.rs | 2 | ||||
| -rw-r--r-- | 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 <pat> = <init>;` or +/// Finds the "init" expression for a pattern: `let <pat> = <init>;` (or `if let`) or /// `match <init> { .., <pat> => .., .. }` 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 |
