Skip to content

Commit

Permalink
Edit based on review recommendations
Browse files Browse the repository at this point in the history
  • Loading branch information
surechen committed Nov 20, 2021
1 parent b9529d7 commit cc4d217
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 63 deletions.
70 changes: 8 additions & 62 deletions clippy_lints/src/shadow.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use clippy_utils::diagnostics::span_lint_and_note;
use clippy_utils::source::snippet;
use clippy_utils::visitors::is_local_used;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def::Res;
use rustc_hir::def_id::LocalDefId;
use rustc_hir::hir_id::ItemLocalId;
use rustc_hir::intravisit::FnKind;
use rustc_hir::{Block, Body, BodyOwnerKind, Expr, ExprKind, FnDecl, HirId, IsAsync, Node, Pat, PatKind, QPath, UnOp};
use rustc_hir::{Block, Body, BodyOwnerKind, Expr, ExprKind, HirId, Node, Pat, PatKind, QPath, UnOp};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::{Span, Symbol};
Expand Down Expand Up @@ -96,7 +95,6 @@ declare_clippy_lint! {
#[derive(Default)]
pub(crate) struct Shadow {
bindings: Vec<FxHashMap<Symbol, Vec<ItemLocalId>>>,
skip: Vec<FxHashSet<Symbol>>,
}

impl_lint_pass!(Shadow => [SHADOW_SAME, SHADOW_REUSE, SHADOW_UNRELATED]);
Expand All @@ -107,11 +105,16 @@ impl<'tcx> LateLintPass<'tcx> for Shadow {
PatKind::Binding(_, hir_id, ident, _) => (hir_id, ident),
_ => return,
};

if pat.span.desugaring_kind().is_some() {
return;
}

if ident.span.from_expansion() || ident.span.is_dummy() {
return;
}
let HirId { owner, local_id } = id;

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 items_with_name = data.entry(ident.name).or_default();
Expand All @@ -123,11 +126,6 @@ impl<'tcx> LateLintPass<'tcx> for Shadow {
return;
}

if !self.skip.is_empty() && self.skip.last_mut().unwrap().contains(&ident.name) {
// skip async function's params
return;
}

if is_shadow(cx, owner, prev, local_id) {
let prev_hir_id = HirId { owner, local_id: prev };
lint_shadow(cx, pat, prev_hir_id, ident.span);
Expand All @@ -152,58 +150,6 @@ impl<'tcx> LateLintPass<'tcx> for Shadow {
self.bindings.pop();
}
}

fn check_fn(
&mut self,
_: &LateContext<'tcx>,
kind: FnKind<'tcx>,
_: &'tcx FnDecl<'_>,
body: &'tcx Body<'_>,
_: Span,
_: HirId,
) {
if_chain! {
if let Some(header) = match kind {
FnKind::ItemFn(_, _, header, _) => Some(header),
FnKind::Method(_, sig, _) => Some(sig.header),
FnKind::Closure => None,
};
if header.asyncness == IsAsync::Async;
if !body.params.is_empty();
then {
self.skip.push(FxHashSet::default());
let skip_params = self.skip.last_mut().unwrap();
for i in body.params {
if let PatKind::Binding(.., ident, _) = i.pat.kind {
skip_params.insert(ident.name);
}
}
}
}
}

fn check_fn_post(
&mut self,
_: &LateContext<'tcx>,
kind: FnKind<'tcx>,
_: &'tcx FnDecl<'_>,
body: &'tcx Body<'_>,
_: Span,
_: HirId,
) {
if_chain! {
if let Some(header) = match kind {
FnKind::ItemFn(_, _, header, _) => Some(header),
FnKind::Method(_, sig, _) => Some(sig.header),
FnKind::Closure => None,
};
if header.asyncness == IsAsync::Async;
if !body.params.is_empty();
then {
self.skip.pop();
}
}
}
}

fn is_shadow(cx: &LateContext<'_>, owner: LocalDefId, first: ItemLocalId, second: ItemLocalId) -> bool {
Expand Down
14 changes: 13 additions & 1 deletion tests/ui/shadow.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -241,5 +241,17 @@ note: previous binding is here
LL | let _ = |[x]: [u32; 1]| {
| ^

error: aborting due to 20 previous errors
error: `_b` shadows a previous, unrelated binding
--> $DIR/shadow.rs:85:9
|
LL | let _b = _a;
| ^^
|
note: previous binding is here
--> $DIR/shadow.rs:84:28
|
LL | pub async fn foo2(_a: i32, _b: i64) {
| ^^

error: aborting due to 21 previous errors

0 comments on commit cc4d217

Please sign in to comment.