Lint shadow_* lints in anon const blocks

This commit is contained in:
Jason Newcomb 2022-07-05 15:21:27 -04:00
parent 462136ac68
commit 3db0e00bdc
2 changed files with 19 additions and 9 deletions

View File

@ -99,7 +99,7 @@
#[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 @@ 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));
}
}

View File

@ -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