Skip to content

Commit cd8f848

Browse files
committed
Do not warn about shadowing in a destructuring assigment
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.
1 parent 1b62885 commit cd8f848

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

clippy_lints/src/shadow.rs

+11-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,14 @@ impl<'tcx> LateLintPass<'tcx> for Shadow {
125127
return;
126128
}
127129

130+
// Desugaring of a destructuring assignment may reuse the same identifier internally
131+
if let Node::Pat(parent_pat) = cx.tcx.parent_hir_node(pat.hir_id)
132+
&& let Node::LetStmt(let_stmt) = cx.tcx.parent_hir_node(parent_pat.hir_id)
133+
&& let LocalSource::AssignDesugar(_) = let_stmt.source
134+
{
135+
return;
136+
}
137+
128138
let HirId { owner, local_id } = id;
129139
// get (or insert) the list of items for this owner and symbol
130140
let (ref mut data, scope_owner) = *self.bindings.last_mut().unwrap();

tests/ui/shadow.rs

+6
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,10 @@ 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+
170176
fn main() {}

0 commit comments

Comments
 (0)