Skip to content

Commit fd605ab

Browse files
committed
Auto merge of rust-lang#9124 - Jarcho:shadow_ice, r=Alexendoo
Lint `shadow_*` lints in anon const blocks changelog: Lint `shadow_*` lints in anon const blocks
2 parents 462136a + 3db0e00 commit fd605ab

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

clippy_lints/src/shadow.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ declare_clippy_lint! {
9999

100100
#[derive(Default)]
101101
pub(crate) struct Shadow {
102-
bindings: Vec<FxHashMap<Symbol, Vec<ItemLocalId>>>,
102+
bindings: Vec<(FxHashMap<Symbol, Vec<ItemLocalId>>, LocalDefId)>,
103103
}
104104

105105
impl_lint_pass!(Shadow => [SHADOW_SAME, SHADOW_REUSE, SHADOW_UNRELATED]);
@@ -121,7 +121,7 @@ impl<'tcx> LateLintPass<'tcx> for Shadow {
121121

122122
let HirId { owner, local_id } = id;
123123
// get (or insert) the list of items for this owner and symbol
124-
let data = self.bindings.last_mut().unwrap();
124+
let (ref mut data, scope_owner) = *self.bindings.last_mut().unwrap();
125125
let items_with_name = data.entry(ident.name).or_default();
126126

127127
// check other bindings with the same name, most recently seen first
@@ -131,7 +131,7 @@ impl<'tcx> LateLintPass<'tcx> for Shadow {
131131
return;
132132
}
133133

134-
if is_shadow(cx, owner, prev, local_id) {
134+
if is_shadow(cx, scope_owner, prev, local_id) {
135135
let prev_hir_id = HirId { owner, local_id: prev };
136136
lint_shadow(cx, pat, prev_hir_id, ident.span);
137137
// only lint against the "nearest" shadowed binding
@@ -144,11 +144,9 @@ impl<'tcx> LateLintPass<'tcx> for Shadow {
144144

145145
fn check_body(&mut self, cx: &LateContext<'_>, body: &Body<'_>) {
146146
let hir = cx.tcx.hir();
147-
if !matches!(
148-
hir.body_owner_kind(hir.body_owner_def_id(body.id())),
149-
BodyOwnerKind::Closure
150-
) {
151-
self.bindings.push(FxHashMap::default());
147+
let owner_id = hir.body_owner_def_id(body.id());
148+
if !matches!(hir.body_owner_kind(owner_id), BodyOwnerKind::Closure) {
149+
self.bindings.push((FxHashMap::default(), owner_id));
152150
}
153151
}
154152

tests/ui/shadow.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,5 +265,17 @@ note: previous binding is here
265265
LL | pub async fn foo2(_a: i32, _b: i64) {
266266
| ^^
267267

268-
error: aborting due to 22 previous errors
268+
error: `x` shadows a previous, unrelated binding
269+
--> $DIR/shadow.rs:94:21
270+
|
271+
LL | if let Some(x) = Some(1) { x } else { 1 }
272+
| ^
273+
|
274+
note: previous binding is here
275+
--> $DIR/shadow.rs:93:13
276+
|
277+
LL | let x = 1;
278+
| ^
279+
280+
error: aborting due to 23 previous errors
269281

0 commit comments

Comments
 (0)