2015-08-21 15:11:34 +00:00
|
|
|
use std::ops::Deref;
|
2015-09-03 14:42:17 +00:00
|
|
|
use rustc_front::hir::*;
|
|
|
|
use reexport::*;
|
2015-08-21 15:11:34 +00:00
|
|
|
use syntax::codemap::Span;
|
2015-09-03 14:42:17 +00:00
|
|
|
use rustc_front::visit::FnKind;
|
2015-08-21 15:11:34 +00:00
|
|
|
|
2015-09-19 02:53:04 +00:00
|
|
|
use rustc::lint::*;
|
2015-08-21 15:11:34 +00:00
|
|
|
use rustc::middle::def::Def::{DefVariant, DefStruct};
|
|
|
|
|
2015-09-02 06:19:47 +00:00
|
|
|
use utils::{in_external_macro, snippet, span_lint, span_note_and_lint};
|
2015-08-21 15:11:34 +00:00
|
|
|
|
|
|
|
declare_lint!(pub SHADOW_SAME, Allow,
|
|
|
|
"rebinding a name to itself, e.g. `let mut x = &mut x`");
|
|
|
|
declare_lint!(pub SHADOW_REUSE, Allow,
|
|
|
|
"rebinding a name to an expression that re-uses the original value, e.g. \
|
|
|
|
`let x = x + 1`");
|
2015-09-08 09:50:04 +00:00
|
|
|
declare_lint!(pub SHADOW_UNRELATED, Allow,
|
2015-08-21 15:11:34 +00:00
|
|
|
"The name is re-bound without even using the original value");
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct ShadowPass;
|
|
|
|
|
|
|
|
impl LintPass for ShadowPass {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
2015-08-21 15:11:34 +00:00
|
|
|
lint_array!(SHADOW_SAME, SHADOW_REUSE, SHADOW_UNRELATED)
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
|
|
|
|
2015-09-19 02:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LateLintPass for ShadowPass {
|
|
|
|
fn check_fn(&mut self, cx: &LateContext, _: FnKind, decl: &FnDecl,
|
2015-08-21 15:11:34 +00:00
|
|
|
block: &Block, _: Span, _: NodeId) {
|
|
|
|
if in_external_macro(cx, block.span) { return; }
|
|
|
|
check_fn(cx, decl, block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-19 02:53:04 +00:00
|
|
|
fn check_fn(cx: &LateContext, decl: &FnDecl, block: &Block) {
|
2015-08-21 15:11:34 +00:00
|
|
|
let mut bindings = Vec::new();
|
|
|
|
for arg in &decl.inputs {
|
|
|
|
if let PatIdent(_, ident, _) = arg.pat.node {
|
2015-09-08 09:50:04 +00:00
|
|
|
bindings.push((ident.node.name, ident.span))
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
check_block(cx, block, &mut bindings);
|
|
|
|
}
|
|
|
|
|
2015-09-19 02:53:04 +00:00
|
|
|
fn check_block(cx: &LateContext, block: &Block, bindings: &mut Vec<(Name, Span)>) {
|
2015-08-21 15:11:34 +00:00
|
|
|
let len = bindings.len();
|
|
|
|
for stmt in &block.stmts {
|
|
|
|
match stmt.node {
|
|
|
|
StmtDecl(ref decl, _) => check_decl(cx, decl, bindings),
|
|
|
|
StmtExpr(ref e, _) | StmtSemi(ref e, _) =>
|
2015-09-03 14:42:17 +00:00
|
|
|
check_expr(cx, e, bindings)
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(ref o) = block.expr { check_expr(cx, o, bindings); }
|
|
|
|
bindings.truncate(len);
|
|
|
|
}
|
|
|
|
|
2015-09-19 02:53:04 +00:00
|
|
|
fn check_decl(cx: &LateContext, decl: &Decl, bindings: &mut Vec<(Name, Span)>) {
|
2015-08-21 15:11:34 +00:00
|
|
|
if in_external_macro(cx, decl.span) { return; }
|
|
|
|
if let DeclLocal(ref local) = decl.node {
|
2015-08-21 15:11:34 +00:00
|
|
|
let Local{ ref pat, ref ty, ref init, id: _, span } = **local;
|
|
|
|
if let &Some(ref t) = ty { check_ty(cx, t, bindings) }
|
2015-09-01 23:36:37 +00:00
|
|
|
if let &Some(ref o) = init {
|
2015-09-01 23:36:37 +00:00
|
|
|
check_expr(cx, o, bindings);
|
|
|
|
check_pat(cx, pat, &Some(o), span, bindings);
|
|
|
|
} else {
|
|
|
|
check_pat(cx, pat, &None, span, bindings);
|
|
|
|
}
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-19 02:53:04 +00:00
|
|
|
fn is_binding(cx: &LateContext, pat: &Pat) -> bool {
|
2015-08-25 18:11:03 +00:00
|
|
|
match cx.tcx.def_map.borrow().get(&pat.id).map(|d| d.full_def()) {
|
|
|
|
Some(DefVariant(..)) | Some(DefStruct(..)) => false,
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-19 02:53:04 +00:00
|
|
|
fn check_pat(cx: &LateContext, pat: &Pat, init: &Option<&Expr>, span: Span,
|
2015-09-08 09:50:04 +00:00
|
|
|
bindings: &mut Vec<(Name, Span)>) {
|
2015-08-21 15:11:34 +00:00
|
|
|
//TODO: match more stuff / destructuring
|
2015-08-21 15:11:34 +00:00
|
|
|
match pat.node {
|
|
|
|
PatIdent(_, ref ident, ref inner) => {
|
|
|
|
let name = ident.node.name;
|
2015-08-25 18:11:03 +00:00
|
|
|
if is_binding(cx, pat) {
|
2015-09-08 09:50:04 +00:00
|
|
|
let mut new_binding = true;
|
|
|
|
for tup in bindings.iter_mut() {
|
|
|
|
if tup.0 == name {
|
|
|
|
lint_shadow(cx, name, span, pat.span, init, tup.1);
|
|
|
|
tup.1 = ident.span;
|
|
|
|
new_binding = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if new_binding {
|
|
|
|
bindings.push((name, ident.span));
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
2015-08-21 15:11:34 +00:00
|
|
|
if let Some(ref p) = *inner { check_pat(cx, p, init, span, bindings); }
|
|
|
|
},
|
|
|
|
//PatEnum(Path, Option<Vec<P<Pat>>>),
|
2015-09-01 23:36:37 +00:00
|
|
|
PatStruct(_, ref pfields, _) =>
|
|
|
|
if let Some(ref init_struct) = *init {
|
|
|
|
if let ExprStruct(_, ref efields, _) = init_struct.node {
|
2015-09-01 23:36:37 +00:00
|
|
|
for field in pfields {
|
|
|
|
let ident = field.node.ident;
|
|
|
|
let efield = efields.iter()
|
|
|
|
.find(|ref f| f.ident.node == ident)
|
|
|
|
.map(|f| &*f.expr);
|
|
|
|
check_pat(cx, &field.node.pat, &efield, span, bindings);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for field in pfields {
|
2015-09-01 23:36:37 +00:00
|
|
|
check_pat(cx, &field.node.pat, init, span, bindings);
|
2015-09-01 23:36:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for field in pfields {
|
|
|
|
check_pat(cx, &field.node.pat, &None, span, bindings);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
PatTup(ref inner) =>
|
2015-09-01 23:36:37 +00:00
|
|
|
if let Some(ref init_tup) = *init {
|
2015-09-01 23:36:37 +00:00
|
|
|
if let ExprTup(ref tup) = init_tup.node {
|
2015-09-01 23:36:37 +00:00
|
|
|
for (i, p) in inner.iter().enumerate() {
|
2015-09-01 23:36:37 +00:00
|
|
|
check_pat(cx, p, &Some(&tup[i]), p.span, bindings);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for p in inner {
|
2015-09-01 23:36:37 +00:00
|
|
|
check_pat(cx, p, init, span, bindings);
|
2015-09-01 23:36:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for p in inner {
|
|
|
|
check_pat(cx, p, &None, span, bindings);
|
|
|
|
}
|
|
|
|
},
|
2015-08-21 15:11:34 +00:00
|
|
|
PatBox(ref inner) => {
|
|
|
|
if let Some(ref initp) = *init {
|
2015-09-02 05:56:13 +00:00
|
|
|
if let ExprBox(_, ref inner_init) = initp.node {
|
2015-09-02 06:19:47 +00:00
|
|
|
check_pat(cx, inner, &Some(&**inner_init), span, bindings);
|
2015-09-02 05:56:13 +00:00
|
|
|
} else {
|
2015-09-02 06:19:47 +00:00
|
|
|
check_pat(cx, inner, init, span, bindings);
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
check_pat(cx, inner, init, span, bindings);
|
|
|
|
}
|
|
|
|
},
|
2015-09-02 05:56:13 +00:00
|
|
|
PatRegion(ref inner, _) =>
|
|
|
|
check_pat(cx, inner, init, span, bindings),
|
2015-08-21 15:11:34 +00:00
|
|
|
//PatVec(Vec<P<Pat>>, Option<P<Pat>>, Vec<P<Pat>>),
|
|
|
|
_ => (),
|
|
|
|
}
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
|
|
|
|
2015-09-19 02:53:04 +00:00
|
|
|
fn lint_shadow<T>(cx: &LateContext, name: Name, span: Span, lspan: Span, init:
|
2015-09-08 09:50:04 +00:00
|
|
|
&Option<T>, prev_span: Span) where T: Deref<Target=Expr> {
|
2015-09-19 02:53:04 +00:00
|
|
|
fn note_orig(cx: &LateContext, lint: &'static Lint, span: Span) {
|
2015-09-08 09:50:04 +00:00
|
|
|
if cx.current_level(lint) != Level::Allow {
|
|
|
|
cx.sess().span_note(span, "previous binding is here");
|
|
|
|
}
|
|
|
|
}
|
2015-09-02 06:19:47 +00:00
|
|
|
if let Some(ref expr) = *init {
|
2015-08-21 15:11:34 +00:00
|
|
|
if is_self_shadow(name, expr) {
|
2015-08-27 05:39:40 +00:00
|
|
|
span_lint(cx, SHADOW_SAME, span, &format!(
|
2015-08-21 15:11:34 +00:00
|
|
|
"{} is shadowed by itself in {}",
|
2015-08-21 15:11:34 +00:00
|
|
|
snippet(cx, lspan, "_"),
|
2015-08-27 05:39:40 +00:00
|
|
|
snippet(cx, expr.span, "..")));
|
2015-09-08 09:50:04 +00:00
|
|
|
note_orig(cx, SHADOW_SAME, prev_span);
|
2015-08-21 15:11:34 +00:00
|
|
|
} else {
|
2015-08-21 15:11:34 +00:00
|
|
|
if contains_self(name, expr) {
|
2015-09-02 06:19:47 +00:00
|
|
|
span_note_and_lint(cx, SHADOW_REUSE, lspan, &format!(
|
2015-08-21 15:11:34 +00:00
|
|
|
"{} is shadowed by {} which reuses the original value",
|
|
|
|
snippet(cx, lspan, "_"),
|
2015-09-02 06:19:47 +00:00
|
|
|
snippet(cx, expr.span, "..")),
|
|
|
|
expr.span, "initialization happens here");
|
2015-09-08 09:50:04 +00:00
|
|
|
note_orig(cx, SHADOW_REUSE, prev_span);
|
2015-08-21 15:11:34 +00:00
|
|
|
} else {
|
2015-09-02 06:19:47 +00:00
|
|
|
span_note_and_lint(cx, SHADOW_UNRELATED, lspan, &format!(
|
|
|
|
"{} is shadowed by {}",
|
2015-08-21 15:11:34 +00:00
|
|
|
snippet(cx, lspan, "_"),
|
2015-09-02 06:19:47 +00:00
|
|
|
snippet(cx, expr.span, "..")),
|
|
|
|
expr.span, "initialization happens here");
|
2015-09-08 09:50:04 +00:00
|
|
|
note_orig(cx, SHADOW_UNRELATED, prev_span);
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
2015-08-21 15:11:34 +00:00
|
|
|
} else {
|
2015-08-27 05:39:40 +00:00
|
|
|
span_lint(cx, SHADOW_UNRELATED, span, &format!(
|
2015-09-02 06:19:47 +00:00
|
|
|
"{} shadows a previous declaration", snippet(cx, lspan, "_")));
|
2015-09-08 09:50:04 +00:00
|
|
|
note_orig(cx, SHADOW_UNRELATED, prev_span);
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-19 02:53:04 +00:00
|
|
|
fn check_expr(cx: &LateContext, expr: &Expr, bindings: &mut Vec<(Name, Span)>) {
|
2015-08-21 15:11:34 +00:00
|
|
|
if in_external_macro(cx, expr.span) { return; }
|
|
|
|
match expr.node {
|
2015-09-19 02:53:04 +00:00
|
|
|
ExprUnary(_, ref e) | ExprField(ref e, _) |
|
2015-08-21 15:11:34 +00:00
|
|
|
ExprTupField(ref e, _) | ExprAddrOf(_, ref e) | ExprBox(None, ref e)
|
|
|
|
=> { check_expr(cx, e, bindings) },
|
|
|
|
ExprBox(Some(ref place), ref e) => {
|
|
|
|
check_expr(cx, place, bindings); check_expr(cx, e, bindings) }
|
|
|
|
ExprBlock(ref block) | ExprLoop(ref block, _) =>
|
|
|
|
{ check_block(cx, block, bindings) },
|
2015-08-21 15:11:34 +00:00
|
|
|
//ExprCall
|
|
|
|
//ExprMethodCall
|
2015-08-21 15:11:34 +00:00
|
|
|
ExprVec(ref v) | ExprTup(ref v) =>
|
|
|
|
for ref e in v { check_expr(cx, e, bindings) },
|
|
|
|
ExprIf(ref cond, ref then, ref otherwise) => {
|
|
|
|
check_expr(cx, cond, bindings);
|
|
|
|
check_block(cx, then, bindings);
|
|
|
|
if let &Some(ref o) = otherwise { check_expr(cx, o, bindings); }
|
|
|
|
},
|
|
|
|
ExprWhile(ref cond, ref block, _) => {
|
|
|
|
check_expr(cx, cond, bindings);
|
|
|
|
check_block(cx, block, bindings);
|
|
|
|
},
|
2015-08-21 15:11:34 +00:00
|
|
|
ExprMatch(ref init, ref arms, _) => {
|
|
|
|
check_expr(cx, init, bindings);
|
2015-08-25 21:48:22 +00:00
|
|
|
let len = bindings.len();
|
2015-08-21 15:11:34 +00:00
|
|
|
for ref arm in arms {
|
|
|
|
for ref pat in &arm.pats {
|
2015-08-21 15:11:34 +00:00
|
|
|
check_pat(cx, &pat, &Some(&**init), pat.span, bindings);
|
2015-09-08 09:50:04 +00:00
|
|
|
//This is ugly, but needed to get the right type
|
|
|
|
if let Some(ref guard) = arm.guard {
|
|
|
|
check_expr(cx, guard, bindings);
|
|
|
|
}
|
|
|
|
check_expr(cx, &arm.body, bindings);
|
|
|
|
bindings.truncate(len);
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
|
|
|
},
|
2015-08-21 15:11:34 +00:00
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-19 02:53:04 +00:00
|
|
|
fn check_ty(cx: &LateContext, ty: &Ty, bindings: &mut Vec<(Name, Span)>) {
|
2015-08-21 15:11:34 +00:00
|
|
|
match ty.node {
|
|
|
|
TyParen(ref sty) | TyObjectSum(ref sty, _) |
|
|
|
|
TyVec(ref sty) => check_ty(cx, sty, bindings),
|
|
|
|
TyFixedLengthVec(ref fty, ref expr) => {
|
|
|
|
check_ty(cx, fty, bindings);
|
|
|
|
check_expr(cx, expr, bindings);
|
|
|
|
},
|
|
|
|
TyPtr(MutTy{ ty: ref mty, .. }) |
|
|
|
|
TyRptr(_, MutTy{ ty: ref mty, .. }) => check_ty(cx, mty, bindings),
|
|
|
|
TyTup(ref tup) => { for ref t in tup { check_ty(cx, t, bindings) } },
|
|
|
|
TyTypeof(ref expr) => check_expr(cx, expr, bindings),
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_self_shadow(name: Name, expr: &Expr) -> bool {
|
|
|
|
match expr.node {
|
|
|
|
ExprBox(_, ref inner) |
|
|
|
|
ExprAddrOf(_, ref inner) => is_self_shadow(name, inner),
|
|
|
|
ExprBlock(ref block) => block.stmts.is_empty() && block.expr.as_ref().
|
|
|
|
map_or(false, |ref e| is_self_shadow(name, e)),
|
|
|
|
ExprUnary(op, ref inner) => (UnUniq == op || UnDeref == op) &&
|
|
|
|
is_self_shadow(name, inner),
|
2015-08-21 15:11:34 +00:00
|
|
|
ExprPath(_, ref path) => path_eq_name(name, path),
|
2015-08-21 15:11:34 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-21 15:11:34 +00:00
|
|
|
fn path_eq_name(name: Name, path: &Path) -> bool {
|
2015-08-27 05:39:40 +00:00
|
|
|
!path.global && path.segments.len() == 1 &&
|
2015-08-21 15:11:34 +00:00
|
|
|
path.segments[0].identifier.name == name
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 15:11:34 +00:00
|
|
|
fn contains_self(name: Name, expr: &Expr) -> bool {
|
|
|
|
match expr.node {
|
2015-09-02 16:17:38 +00:00
|
|
|
// the "self" name itself (maybe)
|
|
|
|
ExprPath(_, ref path) => path_eq_name(name, path),
|
|
|
|
// no subexprs
|
|
|
|
ExprLit(_) => false,
|
|
|
|
// one subexpr
|
2015-09-19 02:53:04 +00:00
|
|
|
ExprUnary(_, ref e) | ExprField(ref e, _) |
|
2015-09-02 16:17:38 +00:00
|
|
|
ExprTupField(ref e, _) | ExprAddrOf(_, ref e) | ExprBox(_, ref e) |
|
|
|
|
ExprCast(ref e, _) =>
|
|
|
|
contains_self(name, e),
|
|
|
|
// two subexprs
|
|
|
|
ExprBinary(_, ref l, ref r) | ExprIndex(ref l, ref r) |
|
|
|
|
ExprAssign(ref l, ref r) | ExprAssignOp(_, ref l, ref r) |
|
|
|
|
ExprRepeat(ref l, ref r) =>
|
2015-08-21 15:11:34 +00:00
|
|
|
contains_self(name, l) || contains_self(name, r),
|
2015-09-02 16:17:38 +00:00
|
|
|
// one optional subexpr
|
|
|
|
ExprRet(ref oe) =>
|
|
|
|
oe.as_ref().map_or(false, |ref e| contains_self(name, e)),
|
|
|
|
// two optional subexprs
|
|
|
|
ExprRange(ref ol, ref or) =>
|
|
|
|
ol.as_ref().map_or(false, |ref e| contains_self(name, e)) ||
|
|
|
|
or.as_ref().map_or(false, |ref e| contains_self(name, e)),
|
|
|
|
// one subblock
|
|
|
|
ExprBlock(ref block) | ExprLoop(ref block, _) |
|
|
|
|
ExprClosure(_, _, ref block) =>
|
2015-08-21 15:11:34 +00:00
|
|
|
contains_block_self(name, block),
|
2015-09-02 16:17:38 +00:00
|
|
|
// one vec
|
|
|
|
ExprMethodCall(_, _, ref v) | ExprVec(ref v) | ExprTup(ref v) =>
|
|
|
|
v.iter().any(|ref a| contains_self(name, a)),
|
|
|
|
// one expr, one vec
|
|
|
|
ExprCall(ref fun, ref args) =>
|
|
|
|
contains_self(name, fun) ||
|
2015-08-21 15:11:34 +00:00
|
|
|
args.iter().any(|ref a| contains_self(name, a)),
|
2015-09-02 16:17:38 +00:00
|
|
|
// special ones
|
2015-08-21 15:11:34 +00:00
|
|
|
ExprIf(ref cond, ref then, ref otherwise) =>
|
|
|
|
contains_self(name, cond) || contains_block_self(name, then) ||
|
|
|
|
otherwise.as_ref().map_or(false, |ref e| contains_self(name, e)),
|
2015-08-21 15:11:34 +00:00
|
|
|
ExprWhile(ref e, ref block, _) =>
|
2015-08-21 15:11:34 +00:00
|
|
|
contains_self(name, e) || contains_block_self(name, block),
|
2015-08-27 05:39:40 +00:00
|
|
|
ExprMatch(ref e, ref arms, _) =>
|
2015-09-02 16:17:38 +00:00
|
|
|
contains_self(name, e) ||
|
|
|
|
arms.iter().any(
|
|
|
|
|ref arm|
|
|
|
|
arm.pats.iter().any(|ref pat| contains_pat_self(name, pat)) ||
|
|
|
|
arm.guard.as_ref().map_or(false, |ref g| contains_self(name, g)) ||
|
|
|
|
contains_self(name, &arm.body)),
|
|
|
|
ExprStruct(_, ref fields, ref other) =>
|
|
|
|
fields.iter().any(|ref f| contains_self(name, &f.expr)) ||
|
|
|
|
other.as_ref().map_or(false, |ref e| contains_self(name, e)),
|
|
|
|
_ => false,
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn contains_block_self(name: Name, block: &Block) -> bool {
|
|
|
|
for stmt in &block.stmts {
|
|
|
|
match stmt.node {
|
|
|
|
StmtDecl(ref decl, _) =>
|
|
|
|
if let DeclLocal(ref local) = decl.node {
|
2015-08-21 15:11:34 +00:00
|
|
|
//TODO: We don't currently handle the case where the name
|
|
|
|
//is shadowed wiithin the block; this means code including this
|
|
|
|
//degenerate pattern will get the wrong warning.
|
2015-08-21 15:11:34 +00:00
|
|
|
if let Some(ref init) = local.init {
|
|
|
|
if contains_self(name, init) { return true; }
|
|
|
|
}
|
|
|
|
},
|
|
|
|
StmtExpr(ref e, _) | StmtSemi(ref e, _) =>
|
2015-09-03 14:42:17 +00:00
|
|
|
if contains_self(name, e) { return true }
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(ref e) = block.expr { contains_self(name, e) } else { false }
|
|
|
|
}
|
2015-08-21 15:11:34 +00:00
|
|
|
|
|
|
|
fn contains_pat_self(name: Name, pat: &Pat) -> bool {
|
|
|
|
match pat.node {
|
|
|
|
PatIdent(_, ref ident, ref inner) => name == ident.node.name ||
|
|
|
|
inner.as_ref().map_or(false, |ref p| contains_pat_self(name, p)),
|
2015-08-27 05:39:40 +00:00
|
|
|
PatEnum(_, ref opats) => opats.as_ref().map_or(false,
|
2015-08-21 15:11:34 +00:00
|
|
|
|pats| pats.iter().any(|p| contains_pat_self(name, p))),
|
|
|
|
PatQPath(_, ref path) => path_eq_name(name, path),
|
|
|
|
PatStruct(_, ref fieldpats, _) => fieldpats.iter().any(
|
|
|
|
|ref fp| contains_pat_self(name, &fp.node.pat)),
|
|
|
|
PatTup(ref ps) => ps.iter().any(|ref p| contains_pat_self(name, p)),
|
|
|
|
PatBox(ref p) |
|
|
|
|
PatRegion(ref p, _) => contains_pat_self(name, p),
|
2015-08-27 05:39:40 +00:00
|
|
|
PatRange(ref from, ref until) =>
|
2015-08-21 15:11:34 +00:00
|
|
|
contains_self(name, from) || contains_self(name, until),
|
|
|
|
PatVec(ref pre, ref opt, ref post) =>
|
2015-08-27 05:39:40 +00:00
|
|
|
pre.iter().any(|ref p| contains_pat_self(name, p)) ||
|
2015-08-21 15:11:34 +00:00
|
|
|
opt.as_ref().map_or(false, |ref p| contains_pat_self(name, p)) ||
|
|
|
|
post.iter().any(|ref p| contains_pat_self(name, p)),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|