about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-07-05 20:04:14 +0000
committerbors <bors@rust-lang.org>2022-07-05 20:04:14 +0000
commitfd605ab7e45570eeaab7ca4ed47bcdd4555064b6 (patch)
tree76be2533f5ce76797f205bce84a7976fe4df930c
parent462136ac688f152004f23cd950b4c42f26efbddb (diff)
parent3db0e00bdc1ebd35aa9f28b83c3ed4de7ba78a25 (diff)
downloadrust-fd605ab7e45570eeaab7ca4ed47bcdd4555064b6.tar.gz
rust-fd605ab7e45570eeaab7ca4ed47bcdd4555064b6.zip
Auto merge of #9124 - Jarcho:shadow_ice, r=Alexendoo
Lint `shadow_*` lints in anon const blocks

changelog: Lint `shadow_*` lints in anon const blocks
-rw-r--r--clippy_lints/src/shadow.rs14
-rw-r--r--tests/ui/shadow.stderr14
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 @@ declare_clippy_lint! {
 
 #[derive(Default)]
 pub(crate) struct Shadow {
-    bindings: Vec<FxHashMap<Symbol, Vec<ItemLocalId>>>,
+    bindings: Vec<(FxHashMap<Symbol, Vec<ItemLocalId>>, LocalDefId)>,
 }
 
 impl_lint_pass!(Shadow => [SHADOW_SAME, SHADOW_REUSE, SHADOW_UNRELATED]);
@@ -121,7 +121,7 @@ impl<'tcx> LateLintPass<'tcx> for Shadow {
 
         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 @@ impl<'tcx> LateLintPass<'tcx> for Shadow {
                 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 @@ impl<'tcx> LateLintPass<'tcx> for Shadow {
 
     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