Skip to content

Commit a895265

Browse files
Do not warn about shadowing in a destructuring assigment (#14381)
When lowering a destructuring assignment from AST to HIR, the compiler will reuse the same identifier name (namely `sym::lhs`) for all the fields. The desugaring must be checked for to avoid a false positive of the `shadow_unrelated` lint. Fix #10279 Fix #14377 changelog: [`shadow_unrelated`]: prevent false positive in destructuring assignments
2 parents 764c1b6 + d9913dd commit a895265

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

Diff for: clippy_lints/src/shadow.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ use rustc_data_structures::fx::FxHashMap;
88
use rustc_hir::def::Res;
99
use rustc_hir::def_id::LocalDefId;
1010
use rustc_hir::hir_id::ItemLocalId;
11-
use rustc_hir::{Block, Body, BodyOwnerKind, Expr, ExprKind, HirId, LetExpr, Node, Pat, PatKind, QPath, UnOp};
11+
use rustc_hir::{
12+
Block, Body, BodyOwnerKind, Expr, ExprKind, HirId, LetExpr, LocalSource, Node, Pat, PatKind, QPath, UnOp,
13+
};
1214
use rustc_lint::{LateContext, LateLintPass};
1315
use rustc_session::impl_lint_pass;
1416
use rustc_span::{Span, Symbol};
@@ -125,6 +127,17 @@ impl<'tcx> LateLintPass<'tcx> for Shadow {
125127
return;
126128
}
127129

130+
// Desugaring of a destructuring assignment may reuse the same identifier internally.
131+
// Peel `Pat` and `PatField` nodes and check if we reach a desugared `Let` assignment.
132+
if let Some((_, Node::LetStmt(let_stmt))) = cx
133+
.tcx
134+
.hir_parent_iter(pat.hir_id)
135+
.find(|(_, node)| !matches!(node, Node::Pat(_) | Node::PatField(_)))
136+
&& let LocalSource::AssignDesugar(_) = let_stmt.source
137+
{
138+
return;
139+
}
140+
128141
let HirId { owner, local_id } = id;
129142
// get (or insert) the list of items for this owner and symbol
130143
let (ref mut data, scope_owner) = *self.bindings.last_mut().unwrap();

Diff for: tests/ui/shadow.rs

+15
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,19 @@ fn issue13795(value: Issue13795) {
167167
//~^ shadow_same
168168
}
169169

170+
fn issue14377() {
171+
let a;
172+
let b;
173+
(a, b) = (0, 1);
174+
175+
struct S {
176+
c: i32,
177+
d: i32,
178+
}
179+
180+
let c;
181+
let d;
182+
S { c, d } = S { c: 1, d: 2 };
183+
}
184+
170185
fn main() {}

0 commit comments

Comments
 (0)