Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
b-ncMN authored May 29, 2022
1 parent 39231b4 commit 5841dbf
Showing 1 changed file with 16 additions and 5 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

0 comments on commit 5841dbf

Please sign in to comment.