Skip to content

Commit cc4d217

Browse files
committed
Edit based on review recommendations
1 parent b9529d7 commit cc4d217

File tree

2 files changed

+21
-63
lines changed

2 files changed

+21
-63
lines changed

clippy_lints/src/shadow.rs

Lines changed: 8 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use clippy_utils::diagnostics::span_lint_and_note;
22
use clippy_utils::source::snippet;
33
use clippy_utils::visitors::is_local_used;
4-
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
4+
use rustc_data_structures::fx::FxHashMap;
55
use rustc_hir::def::Res;
66
use rustc_hir::def_id::LocalDefId;
77
use rustc_hir::hir_id::ItemLocalId;
8-
use rustc_hir::intravisit::FnKind;
9-
use rustc_hir::{Block, Body, BodyOwnerKind, Expr, ExprKind, FnDecl, HirId, IsAsync, Node, Pat, PatKind, QPath, UnOp};
8+
use rustc_hir::{Block, Body, BodyOwnerKind, Expr, ExprKind, HirId, Node, Pat, PatKind, QPath, UnOp};
109
use rustc_lint::{LateContext, LateLintPass};
1110
use rustc_session::{declare_tool_lint, impl_lint_pass};
1211
use rustc_span::{Span, Symbol};
@@ -96,7 +95,6 @@ declare_clippy_lint! {
9695
#[derive(Default)]
9796
pub(crate) struct Shadow {
9897
bindings: Vec<FxHashMap<Symbol, Vec<ItemLocalId>>>,
99-
skip: Vec<FxHashSet<Symbol>>,
10098
}
10199

102100
impl_lint_pass!(Shadow => [SHADOW_SAME, SHADOW_REUSE, SHADOW_UNRELATED]);
@@ -107,11 +105,16 @@ impl<'tcx> LateLintPass<'tcx> for Shadow {
107105
PatKind::Binding(_, hir_id, ident, _) => (hir_id, ident),
108106
_ => return,
109107
};
108+
109+
if pat.span.desugaring_kind().is_some() {
110+
return;
111+
}
112+
110113
if ident.span.from_expansion() || ident.span.is_dummy() {
111114
return;
112115
}
113-
let HirId { owner, local_id } = id;
114116

117+
let HirId { owner, local_id } = id;
115118
// get (or insert) the list of items for this owner and symbol
116119
let data = self.bindings.last_mut().unwrap();
117120
let items_with_name = data.entry(ident.name).or_default();
@@ -123,11 +126,6 @@ impl<'tcx> LateLintPass<'tcx> for Shadow {
123126
return;
124127
}
125128

126-
if !self.skip.is_empty() && self.skip.last_mut().unwrap().contains(&ident.name) {
127-
// skip async function's params
128-
return;
129-
}
130-
131129
if is_shadow(cx, owner, prev, local_id) {
132130
let prev_hir_id = HirId { owner, local_id: prev };
133131
lint_shadow(cx, pat, prev_hir_id, ident.span);
@@ -152,58 +150,6 @@ impl<'tcx> LateLintPass<'tcx> for Shadow {
152150
self.bindings.pop();
153151
}
154152
}
155-
156-
fn check_fn(
157-
&mut self,
158-
_: &LateContext<'tcx>,
159-
kind: FnKind<'tcx>,
160-
_: &'tcx FnDecl<'_>,
161-
body: &'tcx Body<'_>,
162-
_: Span,
163-
_: HirId,
164-
) {
165-
if_chain! {
166-
if let Some(header) = match kind {
167-
FnKind::ItemFn(_, _, header, _) => Some(header),
168-
FnKind::Method(_, sig, _) => Some(sig.header),
169-
FnKind::Closure => None,
170-
};
171-
if header.asyncness == IsAsync::Async;
172-
if !body.params.is_empty();
173-
then {
174-
self.skip.push(FxHashSet::default());
175-
let skip_params = self.skip.last_mut().unwrap();
176-
for i in body.params {
177-
if let PatKind::Binding(.., ident, _) = i.pat.kind {
178-
skip_params.insert(ident.name);
179-
}
180-
}
181-
}
182-
}
183-
}
184-
185-
fn check_fn_post(
186-
&mut self,
187-
_: &LateContext<'tcx>,
188-
kind: FnKind<'tcx>,
189-
_: &'tcx FnDecl<'_>,
190-
body: &'tcx Body<'_>,
191-
_: Span,
192-
_: HirId,
193-
) {
194-
if_chain! {
195-
if let Some(header) = match kind {
196-
FnKind::ItemFn(_, _, header, _) => Some(header),
197-
FnKind::Method(_, sig, _) => Some(sig.header),
198-
FnKind::Closure => None,
199-
};
200-
if header.asyncness == IsAsync::Async;
201-
if !body.params.is_empty();
202-
then {
203-
self.skip.pop();
204-
}
205-
}
206-
}
207153
}
208154

209155
fn is_shadow(cx: &LateContext<'_>, owner: LocalDefId, first: ItemLocalId, second: ItemLocalId) -> bool {

tests/ui/shadow.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,5 +241,17 @@ note: previous binding is here
241241
LL | let _ = |[x]: [u32; 1]| {
242242
| ^
243243

244-
error: aborting due to 20 previous errors
244+
error: `_b` shadows a previous, unrelated binding
245+
--> $DIR/shadow.rs:85:9
246+
|
247+
LL | let _b = _a;
248+
| ^^
249+
|
250+
note: previous binding is here
251+
--> $DIR/shadow.rs:84:28
252+
|
253+
LL | pub async fn foo2(_a: i32, _b: i64) {
254+
| ^^
255+
256+
error: aborting due to 21 previous errors
245257

0 commit comments

Comments
 (0)