2015-09-03 14:42:17 +00:00
|
|
|
use reexport::*;
|
2015-09-19 02:53:04 +00:00
|
|
|
use rustc::lint::*;
|
2016-04-07 15:46:48 +00:00
|
|
|
use rustc::hir::*;
|
2016-12-06 10:32:21 +00:00
|
|
|
use rustc::hir::intravisit::{Visitor, FnKind, NestedVisitorMap};
|
2016-12-01 21:31:56 +00:00
|
|
|
use rustc::ty;
|
2016-02-24 16:38:57 +00:00
|
|
|
use syntax::codemap::Span;
|
2017-01-04 21:46:41 +00:00
|
|
|
use utils::{higher, in_external_macro, snippet, span_lint_and_then, iter_input_pats};
|
2015-08-21 15:11:34 +00:00
|
|
|
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **What it does:** Checks for bindings that shadow other bindings already in
|
|
|
|
/// scope, while just changing reference level or mutability.
|
2015-12-11 00:22:27 +00:00
|
|
|
///
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **Why is this bad?** Not much, in fact it's a very common pattern in Rust
|
|
|
|
/// code. Still, some may opt to avoid it in their code base, they can set this
|
|
|
|
/// lint to `Warn`.
|
2015-12-11 00:22:27 +00:00
|
|
|
///
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **Known problems:** This lint, as the other shadowing related lints,
|
|
|
|
/// currently only catches very simple patterns.
|
2015-12-11 00:22:27 +00:00
|
|
|
///
|
2016-07-15 22:25:44 +00:00
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// let x = &x;
|
|
|
|
/// ```
|
2016-02-05 23:13:29 +00:00
|
|
|
declare_lint! {
|
2016-08-06 08:18:36 +00:00
|
|
|
pub SHADOW_SAME,
|
|
|
|
Allow,
|
2016-02-05 23:13:29 +00:00
|
|
|
"rebinding a name to itself, e.g. `let mut x = &mut x`"
|
|
|
|
}
|
|
|
|
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **What it does:** Checks for bindings that shadow other bindings already in
|
|
|
|
/// scope, while reusing the original value.
|
2015-12-11 00:22:27 +00:00
|
|
|
///
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **Why is this bad?** Not too much, in fact it's a common pattern in Rust
|
|
|
|
/// code. Still, some argue that name shadowing like this hurts readability,
|
|
|
|
/// because a value may be bound to different things depending on position in
|
|
|
|
/// the code.
|
2015-12-11 00:22:27 +00:00
|
|
|
///
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **Known problems:** This lint, as the other shadowing related lints,
|
|
|
|
/// currently only catches very simple patterns.
|
2015-12-11 00:22:27 +00:00
|
|
|
///
|
2016-07-15 22:25:44 +00:00
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// let x = x + 1;
|
|
|
|
/// ```
|
2016-02-05 23:13:29 +00:00
|
|
|
declare_lint! {
|
2016-08-06 08:18:36 +00:00
|
|
|
pub SHADOW_REUSE,
|
|
|
|
Allow,
|
2015-08-21 15:11:34 +00:00
|
|
|
"rebinding a name to an expression that re-uses the original value, e.g. \
|
2016-08-06 08:18:36 +00:00
|
|
|
`let x = x + 1`"
|
2016-02-05 23:13:29 +00:00
|
|
|
}
|
|
|
|
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **What it does:** Checks for bindings that shadow other bindings already in
|
|
|
|
/// scope, either without a initialization or with one that does not even use
|
|
|
|
/// the original value.
|
2015-12-11 00:22:27 +00:00
|
|
|
///
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **Why is this bad?** Name shadowing can hurt readability, especially in
|
|
|
|
/// large code bases, because it is easy to lose track of the active binding at
|
|
|
|
/// any place in the code. This can be alleviated by either giving more specific
|
|
|
|
/// names to bindings ore introducing more scopes to contain the bindings.
|
2015-12-11 00:22:27 +00:00
|
|
|
///
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **Known problems:** This lint, as the other shadowing related lints,
|
|
|
|
/// currently only catches very simple patterns.
|
2015-12-11 00:22:27 +00:00
|
|
|
///
|
2016-07-15 22:25:44 +00:00
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// let x = y; let x = z; // shadows the earlier binding
|
|
|
|
/// ```
|
2016-02-05 23:13:29 +00:00
|
|
|
declare_lint! {
|
2016-08-06 08:18:36 +00:00
|
|
|
pub SHADOW_UNRELATED,
|
|
|
|
Allow,
|
|
|
|
"rebinding a name without even using the original value"
|
2016-02-05 23:13:29 +00:00
|
|
|
}
|
2015-08-21 15:11:34 +00:00
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
2016-06-10 14:17:20 +00:00
|
|
|
pub struct Pass;
|
2015-08-21 15:11:34 +00:00
|
|
|
|
2016-06-10 14:17:20 +00:00
|
|
|
impl LintPass for Pass {
|
2015-08-21 15:11:34 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-12-07 12:13:40 +00:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
|
2016-12-21 11:14:54 +00:00
|
|
|
fn check_fn(
|
|
|
|
&mut self,
|
|
|
|
cx: &LateContext<'a, 'tcx>,
|
|
|
|
_: FnKind<'tcx>,
|
|
|
|
decl: &'tcx FnDecl,
|
2017-01-04 04:40:42 +00:00
|
|
|
body: &'tcx Body,
|
2016-12-21 11:14:54 +00:00
|
|
|
_: Span,
|
|
|
|
_: NodeId
|
|
|
|
) {
|
2017-01-04 04:40:42 +00:00
|
|
|
if in_external_macro(cx, body.value.span) {
|
2016-01-04 04:26:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-01-04 23:48:34 +00:00
|
|
|
check_fn(cx, decl, body);
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 21:46:41 +00:00
|
|
|
fn check_fn<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, decl: &'tcx FnDecl, body: &'tcx Body) {
|
2015-08-21 15:11:34 +00:00
|
|
|
let mut bindings = Vec::new();
|
2017-01-04 21:46:41 +00:00
|
|
|
for arg in iter_input_pats(decl, body) {
|
|
|
|
if let PatKind::Binding(_, _, ident, _) = arg.pat.node {
|
2016-06-15 14:27:56 +00:00
|
|
|
bindings.push((ident.node, ident.span))
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-04 21:46:41 +00:00
|
|
|
check_expr(cx, &body.value, &mut bindings);
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 12:13:40 +00:00
|
|
|
fn check_block<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, block: &'tcx 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),
|
2016-04-14 18:14:03 +00:00
|
|
|
StmtExpr(ref e, _) |
|
|
|
|
StmtSemi(ref e, _) => check_expr(cx, e, bindings),
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-04 04:26:12 +00:00
|
|
|
if let Some(ref o) = block.expr {
|
|
|
|
check_expr(cx, o, bindings);
|
|
|
|
}
|
2015-08-21 15:11:34 +00:00
|
|
|
bindings.truncate(len);
|
|
|
|
}
|
|
|
|
|
2016-12-07 12:13:40 +00:00
|
|
|
fn check_decl<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, decl: &'tcx Decl, bindings: &mut Vec<(Name, Span)>) {
|
2016-01-04 04:26:12 +00:00
|
|
|
if in_external_macro(cx, decl.span) {
|
|
|
|
return;
|
|
|
|
}
|
2016-06-29 22:08:43 +00:00
|
|
|
if higher::is_from_for_desugar(decl) {
|
2016-01-04 04:26:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-08-21 15:11:34 +00:00
|
|
|
if let DeclLocal(ref local) = decl.node {
|
2016-04-14 18:14:03 +00:00
|
|
|
let Local { ref pat, ref ty, ref init, span, .. } = **local;
|
2016-01-04 04:26:12 +00:00
|
|
|
if let Some(ref t) = *ty {
|
|
|
|
check_ty(cx, t, bindings)
|
|
|
|
}
|
2015-11-24 17:44:40 +00:00
|
|
|
if let Some(ref o) = *init {
|
2015-09-01 23:36:37 +00:00
|
|
|
check_expr(cx, o, bindings);
|
2016-12-06 10:32:21 +00:00
|
|
|
check_pat(cx, pat, Some(o), span, bindings);
|
2015-09-01 23:36:37 +00:00
|
|
|
} else {
|
2016-12-06 10:32:21 +00:00
|
|
|
check_pat(cx, pat, None, span, bindings);
|
2015-09-01 23:36:37 +00:00
|
|
|
}
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-01 21:31:56 +00:00
|
|
|
fn is_binding(cx: &LateContext, pat_id: NodeId) -> bool {
|
|
|
|
let var_ty = cx.tcx.tables().node_id_to_type(pat_id);
|
|
|
|
match var_ty.sty {
|
|
|
|
ty::TyAdt(..) => false,
|
2016-01-04 04:26:12 +00:00
|
|
|
_ => true,
|
2015-08-25 18:11:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 11:14:54 +00:00
|
|
|
fn check_pat<'a, 'tcx>(
|
|
|
|
cx: &LateContext<'a, 'tcx>,
|
|
|
|
pat: &'tcx Pat,
|
|
|
|
init: Option<&'tcx Expr>,
|
|
|
|
span: Span,
|
|
|
|
bindings: &mut Vec<(Name, Span)>
|
|
|
|
) {
|
2016-01-04 04:26:12 +00:00
|
|
|
// TODO: match more stuff / destructuring
|
2015-08-21 15:11:34 +00:00
|
|
|
match pat.node {
|
2016-12-01 21:31:56 +00:00
|
|
|
PatKind::Binding(_, _, ref ident, ref inner) => {
|
2016-06-15 14:27:56 +00:00
|
|
|
let name = ident.node;
|
2016-12-01 21:31:56 +00:00
|
|
|
if is_binding(cx, pat.id) {
|
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
|
|
|
}
|
2016-01-04 04:26:12 +00:00
|
|
|
if let Some(ref p) = *inner {
|
|
|
|
check_pat(cx, p, init, span, bindings);
|
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-18 20:16:39 +00:00
|
|
|
PatKind::Struct(_, ref pfields, _) => {
|
2016-12-06 10:32:21 +00:00
|
|
|
if let Some(init_struct) = init {
|
2015-09-01 23:36:37 +00:00
|
|
|
if let ExprStruct(_, ref efields, _) = init_struct.node {
|
2015-09-01 23:36:37 +00:00
|
|
|
for field in pfields {
|
2015-09-24 00:30:39 +00:00
|
|
|
let name = field.node.name;
|
2015-09-01 23:36:37 +00:00
|
|
|
let efield = efields.iter()
|
2016-12-20 17:21:30 +00:00
|
|
|
.find(|f| f.name.node == name)
|
|
|
|
.map(|f| &*f.expr);
|
2016-12-06 10:32:21 +00:00
|
|
|
check_pat(cx, &field.node.pat, efield, span, bindings);
|
2015-09-01 23:36:37 +00:00
|
|
|
}
|
|
|
|
} 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 {
|
2016-12-06 10:32:21 +00:00
|
|
|
check_pat(cx, &field.node.pat, None, span, bindings);
|
2015-09-01 23:36:37 +00:00
|
|
|
}
|
2016-01-04 04:26:12 +00:00
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-05-27 12:24:28 +00:00
|
|
|
PatKind::Tuple(ref inner, _) => {
|
2016-12-06 10:32:21 +00:00
|
|
|
if let Some(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() {
|
2016-12-06 10:32:21 +00:00
|
|
|
check_pat(cx, p, Some(&tup[i]), p.span, bindings);
|
2015-09-01 23:36:37 +00:00
|
|
|
}
|
|
|
|
} 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 {
|
2016-12-06 10:32:21 +00:00
|
|
|
check_pat(cx, p, None, span, bindings);
|
2015-09-01 23:36:37 +00:00
|
|
|
}
|
2016-01-04 04:26:12 +00:00
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-18 20:16:39 +00:00
|
|
|
PatKind::Box(ref inner) => {
|
2016-12-06 10:32:21 +00:00
|
|
|
if let Some(initp) = init {
|
2015-09-25 13:22:36 +00:00
|
|
|
if let ExprBox(ref inner_init) = initp.node {
|
2016-12-06 10:32:21 +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);
|
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-18 20:16:39 +00:00
|
|
|
PatKind::Ref(ref inner, _) => check_pat(cx, inner, init, span, bindings),
|
2016-01-04 04:26:12 +00:00
|
|
|
// PatVec(Vec<P<Pat>>, Option<P<Pat>>, Vec<P<Pat>>),
|
2015-08-21 15:11:34 +00:00
|
|
|
_ => (),
|
|
|
|
}
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 11:14:54 +00:00
|
|
|
fn lint_shadow<'a, 'tcx: 'a>(
|
|
|
|
cx: &LateContext<'a, 'tcx>,
|
|
|
|
name: Name,
|
|
|
|
span: Span,
|
|
|
|
pattern_span: Span,
|
|
|
|
init: Option<&'tcx Expr>,
|
|
|
|
prev_span: Span
|
|
|
|
) {
|
2016-12-06 10:32:21 +00:00
|
|
|
if let Some(expr) = init {
|
2015-08-21 15:11:34 +00:00
|
|
|
if is_self_shadow(name, expr) {
|
2016-06-27 15:14:04 +00:00
|
|
|
span_lint_and_then(cx,
|
2016-01-04 04:26:12 +00:00
|
|
|
SHADOW_SAME,
|
|
|
|
span,
|
2016-05-23 14:34:09 +00:00
|
|
|
&format!("`{}` is shadowed by itself in `{}`",
|
2016-05-11 14:45:06 +00:00
|
|
|
snippet(cx, pattern_span, "_"),
|
2016-06-27 15:14:04 +00:00
|
|
|
snippet(cx, expr.span, "..")),
|
2016-12-20 17:21:30 +00:00
|
|
|
|db| {
|
2016-12-21 09:25:14 +00:00
|
|
|
db.span_note(prev_span, "previous binding is here");
|
|
|
|
});
|
2016-12-06 10:32:21 +00:00
|
|
|
} else if contains_self(cx, name, expr) {
|
2016-06-27 15:14:04 +00:00
|
|
|
span_lint_and_then(cx,
|
|
|
|
SHADOW_REUSE,
|
|
|
|
pattern_span,
|
|
|
|
&format!("`{}` is shadowed by `{}` which reuses the original value",
|
|
|
|
snippet(cx, pattern_span, "_"),
|
|
|
|
snippet(cx, expr.span, "..")),
|
|
|
|
|db| {
|
2016-12-21 09:25:14 +00:00
|
|
|
db.span_note(expr.span, "initialization happens here");
|
|
|
|
db.span_note(prev_span, "previous binding is here");
|
|
|
|
});
|
2015-08-21 15:11:34 +00:00
|
|
|
} else {
|
2016-06-27 15:14:04 +00:00
|
|
|
span_lint_and_then(cx,
|
|
|
|
SHADOW_UNRELATED,
|
|
|
|
pattern_span,
|
|
|
|
&format!("`{}` is shadowed by `{}`",
|
|
|
|
snippet(cx, pattern_span, "_"),
|
|
|
|
snippet(cx, expr.span, "..")),
|
|
|
|
|db| {
|
2016-12-21 09:25:14 +00:00
|
|
|
db.span_note(expr.span, "initialization happens here");
|
|
|
|
db.span_note(prev_span, "previous binding is here");
|
|
|
|
});
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
2016-01-13 17:32:05 +00:00
|
|
|
|
2015-08-21 15:11:34 +00:00
|
|
|
} else {
|
2016-06-27 15:14:04 +00:00
|
|
|
span_lint_and_then(cx,
|
2016-01-04 04:26:12 +00:00
|
|
|
SHADOW_UNRELATED,
|
|
|
|
span,
|
2016-10-02 00:38:26 +00:00
|
|
|
&format!("`{}` shadows a previous declaration", snippet(cx, pattern_span, "_")),
|
2016-12-20 17:21:30 +00:00
|
|
|
|db| {
|
2016-12-21 09:25:14 +00:00
|
|
|
db.span_note(prev_span, "previous binding is here");
|
|
|
|
});
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 12:13:40 +00:00
|
|
|
fn check_expr<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, bindings: &mut Vec<(Name, Span)>) {
|
2016-01-04 04:26:12 +00:00
|
|
|
if in_external_macro(cx, expr.span) {
|
|
|
|
return;
|
|
|
|
}
|
2015-08-21 15:11:34 +00:00
|
|
|
match expr.node {
|
2016-01-04 04:26:12 +00:00
|
|
|
ExprUnary(_, ref e) |
|
|
|
|
ExprField(ref e, _) |
|
|
|
|
ExprTupField(ref e, _) |
|
|
|
|
ExprAddrOf(_, ref e) |
|
|
|
|
ExprBox(ref e) => check_expr(cx, e, bindings),
|
2016-04-14 18:14:03 +00:00
|
|
|
ExprBlock(ref block) |
|
2016-11-25 18:24:55 +00:00
|
|
|
ExprLoop(ref block, _, _) => check_block(cx, block, bindings),
|
2016-01-04 04:26:12 +00:00
|
|
|
// ExprCall
|
|
|
|
// ExprMethodCall
|
2016-09-30 13:35:24 +00:00
|
|
|
ExprArray(ref v) | ExprTup(ref v) => {
|
2016-08-01 14:59:14 +00:00
|
|
|
for e in v {
|
2016-01-04 04:26:12 +00:00
|
|
|
check_expr(cx, e, bindings)
|
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2015-08-21 15:11:34 +00:00
|
|
|
ExprIf(ref cond, ref then, ref otherwise) => {
|
|
|
|
check_expr(cx, cond, bindings);
|
|
|
|
check_block(cx, then, bindings);
|
2016-01-04 04:26:12 +00:00
|
|
|
if let Some(ref o) = *otherwise {
|
|
|
|
check_expr(cx, o, bindings);
|
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2015-08-21 15:11:34 +00:00
|
|
|
ExprWhile(ref cond, ref block, _) => {
|
|
|
|
check_expr(cx, cond, bindings);
|
|
|
|
check_block(cx, block, bindings);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
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();
|
2016-08-01 14:59:14 +00:00
|
|
|
for arm in arms {
|
|
|
|
for pat in &arm.pats {
|
2016-12-06 10:32:21 +00:00
|
|
|
check_pat(cx, pat, Some(&**init), pat.span, bindings);
|
2016-01-04 04:26:12 +00:00
|
|
|
// This is ugly, but needed to get the right type
|
2015-09-08 09:50:04 +00:00
|
|
|
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
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-01-04 04:26:12 +00:00
|
|
|
_ => (),
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 12:13:40 +00:00
|
|
|
fn check_ty<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: &'tcx Ty, bindings: &mut Vec<(Name, Span)>) {
|
2015-08-21 15:11:34 +00:00
|
|
|
match ty.node {
|
2015-11-19 14:51:30 +00:00
|
|
|
TyObjectSum(ref sty, _) |
|
2016-09-30 13:35:24 +00:00
|
|
|
TySlice(ref sty) => check_ty(cx, sty, bindings),
|
2017-01-04 22:18:11 +00:00
|
|
|
TyArray(ref fty, body_id) => {
|
2015-08-21 15:11:34 +00:00
|
|
|
check_ty(cx, fty, bindings);
|
2017-01-04 22:18:11 +00:00
|
|
|
check_expr(cx, &cx.tcx.map.body(body_id).value, bindings);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-04-14 18:14:03 +00:00
|
|
|
TyPtr(MutTy { ty: ref mty, .. }) |
|
|
|
|
TyRptr(_, MutTy { ty: ref mty, .. }) => check_ty(cx, mty, bindings),
|
2016-01-04 04:26:12 +00:00
|
|
|
TyTup(ref tup) => {
|
2016-08-01 14:59:14 +00:00
|
|
|
for t in tup {
|
2016-01-04 04:26:12 +00:00
|
|
|
check_ty(cx, t, bindings)
|
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2017-01-04 22:18:11 +00:00
|
|
|
TyTypeof(body_id) => check_expr(cx, &cx.tcx.map.body(body_id).value, bindings),
|
2015-08-21 15:11:34 +00:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_self_shadow(name: Name, expr: &Expr) -> bool {
|
|
|
|
match expr.node {
|
2015-09-25 13:22:36 +00:00
|
|
|
ExprBox(ref inner) |
|
2015-08-21 15:11:34 +00:00
|
|
|
ExprAddrOf(_, ref inner) => is_self_shadow(name, inner),
|
2016-01-04 04:26:12 +00:00
|
|
|
ExprBlock(ref block) => {
|
2016-08-01 14:59:14 +00:00
|
|
|
block.stmts.is_empty() && block.expr.as_ref().map_or(false, |e| is_self_shadow(name, e))
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-01-04 04:26:12 +00:00
|
|
|
ExprUnary(op, ref inner) => (UnDeref == op) && is_self_shadow(name, inner),
|
2016-12-01 21:31:56 +00:00
|
|
|
ExprPath(QPath::Resolved(_, 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 {
|
2017-01-04 04:40:42 +00:00
|
|
|
!path.is_global() && path.segments.len() == 1 && path.segments[0].name.as_str() == name.as_str()
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 10:32:21 +00:00
|
|
|
struct ContainsSelf<'a, 'tcx: 'a> {
|
2015-11-10 09:25:21 +00:00
|
|
|
name: Name,
|
2016-01-04 04:26:12 +00:00
|
|
|
result: bool,
|
2016-12-06 10:32:21 +00:00
|
|
|
cx: &'a LateContext<'a, 'tcx>,
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 10:32:21 +00:00
|
|
|
impl<'a, 'tcx: 'a> Visitor<'tcx> for ContainsSelf<'a, 'tcx> {
|
2016-05-19 21:14:34 +00:00
|
|
|
fn visit_name(&mut self, _: Span, name: Name) {
|
2016-06-15 14:27:56 +00:00
|
|
|
if self.name == name {
|
2015-11-10 09:25:21 +00:00
|
|
|
self.result = true;
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-06 10:32:21 +00:00
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
|
|
|
|
NestedVisitorMap::All(&self.cx.tcx.map)
|
|
|
|
}
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
2015-08-21 15:11:34 +00:00
|
|
|
|
2016-12-06 10:32:21 +00:00
|
|
|
fn contains_self<'a, 'tcx: 'a>(cx: &LateContext<'a, 'tcx>, name: Name, expr: &'tcx Expr) -> bool {
|
2016-01-04 04:26:12 +00:00
|
|
|
let mut cs = ContainsSelf {
|
|
|
|
name: name,
|
|
|
|
result: false,
|
2016-12-06 10:32:21 +00:00
|
|
|
cx: cx,
|
2016-01-04 04:26:12 +00:00
|
|
|
};
|
2015-11-10 09:25:21 +00:00
|
|
|
cs.visit_expr(expr);
|
|
|
|
cs.result
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|