Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
b-ncMN authored and infrandomness committed May 29, 2022
1 parent 39231b4 commit 4b0a829
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 15 deletions.
21 changes: 16 additions & 5 deletions clippy_lints/src/shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl<'tcx> LateLintPass<'tcx> for Shadow {
return;
}

if is_shadow(cx, owner, prev, local_id) {
if is_shadow(cx, owner, prev, local_id).is_ok() {
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
Expand Down Expand Up @@ -158,11 +158,22 @@ impl<'tcx> LateLintPass<'tcx> for Shadow {
}
}

fn is_shadow(cx: &LateContext<'_>, owner: LocalDefId, first: ItemLocalId, second: ItemLocalId) -> bool {
type Result<T> = std::result::Result<T, LifetimeError>;

#[derive(Debug, Clone)]
struct LifetimeError;

impl std::fmt::Display for LifetimeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Invalid lifetime")
}
}

fn is_shadow(cx: &LateContext<'_>, owner: LocalDefId, first: ItemLocalId, second: ItemLocalId) -> Result<bool> {
let scope_tree = cx.tcx.region_scope_tree(owner.to_def_id());
let first_scope = scope_tree.var_scope(first).unwrap();
let second_scope = scope_tree.var_scope(second).unwrap();
scope_tree.is_subscope_of(second_scope, first_scope)
let first_scope = scope_tree.var_scope(first).ok_or(LifetimeError)?;
let second_scope = scope_tree.var_scope(second).ok_or(LifetimeError)?;
Ok(scope_tree.is_subscope_of(second_scope, first_scope))
}

fn lint_shadow(cx: &LateContext<'_>, pat: &Pat<'_>, shadowed: HirId, span: Span) {
Expand Down
92 changes: 82 additions & 10 deletions tests/ui/shadow.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,10 @@ LL | if let Some(x) = Some(1) {}
| ^
|
note: previous binding is here
--> $DIR/shadow.rs:38:9
--> $DIR/shadow.rs:42:17
|
LL | let x = 1;
| ^
LL | let x = 1;
| ^

error: `x` shadows a previous, unrelated binding
--> $DIR/shadow.rs:47:20
Expand All @@ -212,10 +212,10 @@ LL | while let Some(x) = Some(1) {}
| ^
|
note: previous binding is here
--> $DIR/shadow.rs:38:9
--> $DIR/shadow.rs:46:17
|
LL | let x = 1;
| ^
LL | if let Some(x) = Some(1) {}
| ^

error: `x` shadows a previous, unrelated binding
--> $DIR/shadow.rs:48:15
Expand All @@ -224,10 +224,10 @@ LL | let _ = |[x]: [u32; 1]| {
| ^
|
note: previous binding is here
--> $DIR/shadow.rs:38:9
--> $DIR/shadow.rs:47:20
|
LL | let x = 1;
| ^
LL | while let Some(x) = Some(1) {}
| ^

error: `x` shadows a previous, unrelated binding
--> $DIR/shadow.rs:49:13
Expand All @@ -253,6 +253,78 @@ note: previous binding is here
LL | let y = Some(1);
| ^

error: `x` shadows a previous, unrelated binding
--> $DIR/shadow.rs:58:14
|
LL | Some(x) => {},
| ^
|
note: previous binding is here
--> $DIR/shadow.rs:57:14
|
LL | Some(x) if x == 1 => {},
| ^

error: `x` shadows a previous, unrelated binding
--> $DIR/shadow.rs:62:15
|
LL | [Some(x), None] | [None, Some(x)] => {},
| ^
|
note: previous binding is here
--> $DIR/shadow.rs:58:14
|
LL | Some(x) => {},
| ^

error: `x` shadows a previous, unrelated binding
--> $DIR/shadow.rs:65:17
|
LL | if let Some(x) = Some(1) {
| ^
|
note: previous binding is here
--> $DIR/shadow.rs:62:15
|
LL | [Some(x), None] | [None, Some(x)] => {},
| ^

error: `x` shadows a previous, unrelated binding
--> $DIR/shadow.rs:68:13
|
LL | let x = 1;
| ^
|
note: previous binding is here
--> $DIR/shadow.rs:65:17
|
LL | if let Some(x) = Some(1) {
| ^

error: `y` shadows a previous, unrelated binding
--> $DIR/shadow.rs:69:13
|
LL | let y = 1;
| ^
|
note: previous binding is here
--> $DIR/shadow.rs:66:13
|
LL | let y = 1;
| ^

error: `x` shadows a previous, unrelated binding
--> $DIR/shadow.rs:71:9
|
LL | let x = 1;
| ^
|
note: previous binding is here
--> $DIR/shadow.rs:68:13
|
LL | let x = 1;
| ^

error: `_b` shadows a previous, unrelated binding
--> $DIR/shadow.rs:88:9
|
Expand All @@ -265,5 +337,5 @@ note: previous binding is here
LL | pub async fn foo2(_a: i32, _b: i64) {
| ^^

error: aborting due to 22 previous errors
error: aborting due to 28 previous errors

0 comments on commit 4b0a829

Please sign in to comment.