From 3db0e00bdc1ebd35aa9f28b83c3ed4de7ba78a25 Mon Sep 17 00:00:00 2001 From: Jason Newcomb Date: Tue, 5 Jul 2022 15:21:27 -0400 Subject: [PATCH] Lint `shadow_*` lints in anon const blocks --- clippy_lints/src/shadow.rs | 14 ++++++-------- tests/ui/shadow.stderr | 14 +++++++++++++- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/clippy_lints/src/shadow.rs b/clippy_lints/src/shadow.rs index bf318c055da..5dcdab5b8ab 100644 --- a/clippy_lints/src/shadow.rs +++ b/clippy_lints/src/shadow.rs @@ -99,7 +99,7 @@ #[derive(Default)] pub(crate) struct Shadow { - bindings: Vec>>, + bindings: Vec<(FxHashMap>, LocalDefId)>, } impl_lint_pass!(Shadow => [SHADOW_SAME, SHADOW_REUSE, SHADOW_UNRELATED]); @@ -121,7 +121,7 @@ fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) { let HirId { owner, local_id } = id; // get (or insert) the list of items for this owner and symbol - let data = self.bindings.last_mut().unwrap(); + let (ref mut data, scope_owner) = *self.bindings.last_mut().unwrap(); let items_with_name = data.entry(ident.name).or_default(); // check other bindings with the same name, most recently seen first @@ -131,7 +131,7 @@ fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) { return; } - if is_shadow(cx, owner, prev, local_id) { + if is_shadow(cx, scope_owner, prev, local_id) { let prev_hir_id = HirId { owner, local_id: prev }; lint_shadow(cx, pat, prev_hir_id, ident.span); // only lint against the "nearest" shadowed binding @@ -144,11 +144,9 @@ fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) { fn check_body(&mut self, cx: &LateContext<'_>, body: &Body<'_>) { let hir = cx.tcx.hir(); - if !matches!( - hir.body_owner_kind(hir.body_owner_def_id(body.id())), - BodyOwnerKind::Closure - ) { - self.bindings.push(FxHashMap::default()); + let owner_id = hir.body_owner_def_id(body.id()); + if !matches!(hir.body_owner_kind(owner_id), BodyOwnerKind::Closure) { + self.bindings.push((FxHashMap::default(), owner_id)); } } diff --git a/tests/ui/shadow.stderr b/tests/ui/shadow.stderr index 3bd41d06260..43d76094d0e 100644 --- a/tests/ui/shadow.stderr +++ b/tests/ui/shadow.stderr @@ -265,5 +265,17 @@ note: previous binding is here LL | pub async fn foo2(_a: i32, _b: i64) { | ^^ -error: aborting due to 22 previous errors +error: `x` shadows a previous, unrelated binding + --> $DIR/shadow.rs:94:21 + | +LL | if let Some(x) = Some(1) { x } else { 1 } + | ^ + | +note: previous binding is here + --> $DIR/shadow.rs:93:13 + | +LL | let x = 1; + | ^ + +error: aborting due to 23 previous errors