2018-07-24 06:55:38 +00:00
|
|
|
use crate::utils::{contains_name, higher, iter_input_pats, snippet, span_lint_and_then};
|
2020-01-09 07:13:22 +00:00
|
|
|
use rustc_hir::intravisit::FnKind;
|
2020-02-21 08:39:38 +00:00
|
|
|
use rustc_hir::{
|
|
|
|
Block, Body, Expr, ExprKind, FnDecl, Guard, HirId, Local, MutTy, Pat, PatKind, Path, QPath, StmtKind, Ty, TyKind,
|
|
|
|
UnOp,
|
|
|
|
};
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
2020-03-30 09:02:14 +00:00
|
|
|
use rustc_middle::lint::in_external_macro;
|
|
|
|
use rustc_middle::ty;
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2020-01-04 10:00:00 +00:00
|
|
|
use rustc_span::source_map::Span;
|
2020-08-11 13:43:21 +00:00
|
|
|
use rustc_span::symbol::Symbol;
|
2015-08-21 15:11:34 +00:00
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Checks for bindings that shadow other bindings already in
|
|
|
|
/// scope, while just changing reference level or mutability.
|
|
|
|
///
|
|
|
|
/// **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`.
|
|
|
|
///
|
|
|
|
/// **Known problems:** This lint, as the other shadowing related lints,
|
|
|
|
/// currently only catches very simple patterns.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
2019-08-03 19:24:50 +00:00
|
|
|
/// # let x = 1;
|
2020-06-09 14:36:01 +00:00
|
|
|
/// // Bad
|
2019-03-05 16:50:33 +00:00
|
|
|
/// let x = &x;
|
2020-06-09 14:36:01 +00:00
|
|
|
///
|
|
|
|
/// // Good
|
|
|
|
/// let y = &x; // use different variable name
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```
|
2016-08-06 08:18:36 +00:00
|
|
|
pub SHADOW_SAME,
|
2018-03-28 13:24:26 +00:00
|
|
|
restriction,
|
2019-01-31 01:15:29 +00:00
|
|
|
"rebinding a name to itself, e.g., `let mut x = &mut x`"
|
2016-02-05 23:13:29 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Checks for bindings that shadow other bindings already in
|
|
|
|
/// scope, while reusing the original value.
|
|
|
|
///
|
|
|
|
/// **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.
|
|
|
|
///
|
|
|
|
/// **Known problems:** This lint, as the other shadowing related lints,
|
|
|
|
/// currently only catches very simple patterns.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
2019-08-03 19:24:50 +00:00
|
|
|
/// let x = 2;
|
2019-03-05 16:50:33 +00:00
|
|
|
/// let x = x + 1;
|
|
|
|
/// ```
|
|
|
|
/// use different variable name:
|
|
|
|
/// ```rust
|
2019-08-03 19:24:50 +00:00
|
|
|
/// let x = 2;
|
2019-03-05 16:50:33 +00:00
|
|
|
/// let y = x + 1;
|
|
|
|
/// ```
|
2018-11-27 20:49:09 +00:00
|
|
|
pub SHADOW_REUSE,
|
|
|
|
restriction,
|
2019-01-31 01:15:29 +00:00
|
|
|
"rebinding a name to an expression that re-uses the original value, e.g., `let x = x + 1`"
|
2016-02-05 23:13:29 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +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.
|
|
|
|
///
|
|
|
|
/// **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 or introducing more scopes to contain the bindings.
|
|
|
|
///
|
|
|
|
/// **Known problems:** This lint, as the other shadowing related lints,
|
2020-09-24 12:49:22 +00:00
|
|
|
/// currently only catches very simple patterns. Note that
|
|
|
|
/// `allow`/`warn`/`deny`/`forbid` attributes only work on the function level
|
|
|
|
/// for this lint.
|
2019-03-05 16:50:33 +00:00
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
2019-08-03 16:42:05 +00:00
|
|
|
/// # let y = 1;
|
|
|
|
/// # let z = 2;
|
2019-03-05 16:50:33 +00:00
|
|
|
/// let x = y;
|
2020-06-09 14:36:01 +00:00
|
|
|
///
|
|
|
|
/// // Bad
|
2019-03-05 16:50:33 +00:00
|
|
|
/// let x = z; // shadows the earlier binding
|
2020-06-09 14:36:01 +00:00
|
|
|
///
|
|
|
|
/// // Good
|
|
|
|
/// let w = z; // use different variable name
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```
|
2016-08-06 08:18:36 +00:00
|
|
|
pub SHADOW_UNRELATED,
|
2018-08-09 16:26:22 +00:00
|
|
|
pedantic,
|
2016-08-06 08:18:36 +00:00
|
|
|
"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
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
declare_lint_pass!(Shadow => [SHADOW_SAME, SHADOW_REUSE, SHADOW_UNRELATED]);
|
2015-08-21 15:11:34 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for Shadow {
|
2016-12-21 11:14:54 +00:00
|
|
|
fn check_fn(
|
|
|
|
&mut self,
|
2020-06-25 20:41:36 +00:00
|
|
|
cx: &LateContext<'tcx>,
|
2016-12-21 11:14:54 +00:00
|
|
|
_: FnKind<'tcx>,
|
2019-12-30 04:02:10 +00:00
|
|
|
decl: &'tcx FnDecl<'_>,
|
2019-12-22 14:42:41 +00:00
|
|
|
body: &'tcx Body<'_>,
|
2016-12-21 11:14:54 +00:00
|
|
|
_: Span,
|
2019-02-20 10:11:11 +00:00
|
|
|
_: HirId,
|
2016-12-21 11:14:54 +00:00
|
|
|
) {
|
2018-07-24 06:55:38 +00:00
|
|
|
if in_external_macro(cx.sess(), 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_fn<'tcx>(cx: &LateContext<'tcx>, decl: &'tcx FnDecl<'_>, body: &'tcx Body<'_>) {
|
2020-02-18 11:50:10 +00:00
|
|
|
let mut bindings = Vec::with_capacity(decl.inputs.len());
|
2017-01-04 21:46:41 +00:00
|
|
|
for arg in iter_input_pats(decl, body) {
|
2019-09-27 15:16:06 +00:00
|
|
|
if let PatKind::Binding(.., ident, _) = arg.pat.kind {
|
2018-06-28 13:46:58 +00:00
|
|
|
bindings.push((ident.name, 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
|
|
|
}
|
|
|
|
|
2020-08-11 13:43:21 +00:00
|
|
|
fn check_block<'tcx>(cx: &LateContext<'tcx>, block: &'tcx Block<'_>, bindings: &mut Vec<(Symbol, Span)>) {
|
2015-08-21 15:11:34 +00:00
|
|
|
let len = bindings.len();
|
2019-12-27 07:12:26 +00:00
|
|
|
for stmt in block.stmts {
|
2019-09-27 15:16:06 +00:00
|
|
|
match stmt.kind {
|
2019-01-20 10:21:30 +00:00
|
|
|
StmtKind::Local(ref local) => check_local(cx, local, bindings),
|
|
|
|
StmtKind::Expr(ref e) | StmtKind::Semi(ref e) => check_expr(cx, e, bindings),
|
|
|
|
StmtKind::Item(..) => {},
|
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);
|
|
|
|
}
|
|
|
|
|
2020-08-11 13:43:21 +00:00
|
|
|
fn check_local<'tcx>(cx: &LateContext<'tcx>, local: &'tcx Local<'_>, bindings: &mut Vec<(Symbol, Span)>) {
|
2019-01-20 10:21:30 +00:00
|
|
|
if in_external_macro(cx.sess(), local.span) {
|
2016-01-04 04:26:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-01-20 10:21:30 +00:00
|
|
|
if higher::is_from_for_desugar(local) {
|
2016-01-04 04:26:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-01-20 10:49:45 +00:00
|
|
|
let Local {
|
|
|
|
ref pat,
|
|
|
|
ref ty,
|
|
|
|
ref init,
|
|
|
|
span,
|
|
|
|
..
|
|
|
|
} = *local;
|
|
|
|
if let Some(ref t) = *ty {
|
|
|
|
check_ty(cx, t, bindings)
|
|
|
|
}
|
|
|
|
if let Some(ref o) = *init {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn is_binding(cx: &LateContext<'_>, pat_id: HirId) -> bool {
|
2020-07-17 08:47:04 +00:00
|
|
|
let var_ty = cx.typeck_results().node_type_opt(pat_id);
|
2020-08-03 22:18:29 +00:00
|
|
|
var_ty.map_or(false, |var_ty| !matches!(var_ty.kind(), ty::Adt(..)))
|
2015-08-25 18:11:03 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_pat<'tcx>(
|
|
|
|
cx: &LateContext<'tcx>,
|
2019-12-27 07:12:26 +00:00
|
|
|
pat: &'tcx Pat<'_>,
|
|
|
|
init: Option<&'tcx Expr<'_>>,
|
2016-12-21 11:14:54 +00:00
|
|
|
span: Span,
|
2020-08-11 13:43:21 +00:00
|
|
|
bindings: &mut Vec<(Symbol, Span)>,
|
2016-12-21 11:14:54 +00:00
|
|
|
) {
|
2016-01-04 04:26:12 +00:00
|
|
|
// TODO: match more stuff / destructuring
|
2019-09-27 15:16:06 +00:00
|
|
|
match pat.kind {
|
2019-02-03 07:12:07 +00:00
|
|
|
PatKind::Binding(.., ident, ref inner) => {
|
2018-06-28 13:46:58 +00:00
|
|
|
let name = ident.name;
|
2017-08-15 09:10:49 +00:00
|
|
|
if is_binding(cx, pat.hir_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
|
|
|
},
|
2019-12-27 07:12:26 +00:00
|
|
|
PatKind::Struct(_, pfields, _) => {
|
2018-11-27 20:14:15 +00:00
|
|
|
if let Some(init_struct) = init {
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ExprKind::Struct(_, ref efields, _) = init_struct.kind {
|
2018-11-27 20:14:15 +00:00
|
|
|
for field in pfields {
|
2019-08-16 02:30:38 +00:00
|
|
|
let name = field.ident.name;
|
2019-04-28 18:19:05 +00:00
|
|
|
let efield = efields
|
|
|
|
.iter()
|
|
|
|
.find_map(|f| if f.ident.name == name { Some(&*f.expr) } else { None });
|
2019-08-16 02:30:38 +00:00
|
|
|
check_pat(cx, &field.pat, efield, span, bindings);
|
2018-11-27 20:14:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for field in pfields {
|
2019-08-16 02:30:38 +00:00
|
|
|
check_pat(cx, &field.pat, init, span, bindings);
|
2018-11-27 20:14:15 +00:00
|
|
|
}
|
2015-09-01 23:36:37 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for field in pfields {
|
2019-08-16 02:30:38 +00:00
|
|
|
check_pat(cx, &field.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
|
|
|
},
|
2019-12-27 07:12:26 +00:00
|
|
|
PatKind::Tuple(inner, _) => {
|
2018-11-27 20:14:15 +00:00
|
|
|
if let Some(init_tup) = init {
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ExprKind::Tup(ref tup) = init_tup.kind {
|
2018-11-27 20:14:15 +00:00
|
|
|
for (i, p) in inner.iter().enumerate() {
|
|
|
|
check_pat(cx, p, Some(&tup[i]), p.span, bindings);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for p in inner {
|
|
|
|
check_pat(cx, p, init, span, bindings);
|
|
|
|
}
|
2015-09-01 23:36:37 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for p in inner {
|
2018-11-27 20:14:15 +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
|
|
|
},
|
2018-11-27 20:14:15 +00:00
|
|
|
PatKind::Box(ref inner) => {
|
|
|
|
if let Some(initp) = init {
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ExprKind::Box(ref inner_init) = initp.kind {
|
2018-11-27 20:14:15 +00:00
|
|
|
check_pat(cx, inner, Some(&**inner_init), span, bindings);
|
|
|
|
} else {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn lint_shadow<'tcx>(
|
|
|
|
cx: &LateContext<'tcx>,
|
2020-08-11 13:43:21 +00:00
|
|
|
name: Symbol,
|
2016-12-21 11:14:54 +00:00
|
|
|
span: Span,
|
|
|
|
pattern_span: Span,
|
2019-12-27 07:12:26 +00:00
|
|
|
init: Option<&'tcx Expr<'_>>,
|
2017-08-09 07:30:56 +00:00
|
|
|
prev_span: Span,
|
2016-12-21 11:14:54 +00:00
|
|
|
) {
|
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) {
|
2017-08-09 07:30:56 +00:00
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
SHADOW_SAME,
|
|
|
|
span,
|
|
|
|
&format!(
|
|
|
|
"`{}` is shadowed by itself in `{}`",
|
|
|
|
snippet(cx, pattern_span, "_"),
|
|
|
|
snippet(cx, expr.span, "..")
|
|
|
|
),
|
2020-04-17 06:08:00 +00:00
|
|
|
|diag| {
|
|
|
|
diag.span_note(prev_span, "previous binding is here");
|
2017-11-04 19:55:56 +00:00
|
|
|
},
|
2017-08-09 07:30:56 +00:00
|
|
|
);
|
2017-08-23 15:54:35 +00:00
|
|
|
} else if contains_name(name, expr) {
|
2017-08-09 07:30:56 +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, "..")
|
|
|
|
),
|
2020-04-17 06:08:00 +00:00
|
|
|
|diag| {
|
|
|
|
diag.span_note(expr.span, "initialization happens here");
|
|
|
|
diag.span_note(prev_span, "previous binding is here");
|
2017-08-09 07:30:56 +00:00
|
|
|
},
|
|
|
|
);
|
2015-08-21 15:11:34 +00:00
|
|
|
} else {
|
2017-08-09 07:30:56 +00:00
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
SHADOW_UNRELATED,
|
|
|
|
pattern_span,
|
2020-07-26 19:07:07 +00:00
|
|
|
&format!("`{}` is being shadowed", snippet(cx, pattern_span, "_")),
|
2020-04-17 06:08:00 +00:00
|
|
|
|diag| {
|
|
|
|
diag.span_note(expr.span, "initialization happens here");
|
|
|
|
diag.span_note(prev_span, "previous binding is here");
|
2017-08-09 07:30:56 +00:00
|
|
|
},
|
|
|
|
);
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
2015-08-21 15:11:34 +00:00
|
|
|
} else {
|
2017-09-05 09:33:04 +00:00
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
SHADOW_UNRELATED,
|
|
|
|
span,
|
|
|
|
&format!("`{}` shadows a previous declaration", snippet(cx, pattern_span, "_")),
|
2020-04-17 06:08:00 +00:00
|
|
|
|diag| {
|
|
|
|
diag.span_note(prev_span, "previous binding is here");
|
2017-11-04 19:55:56 +00:00
|
|
|
},
|
2017-09-05 09:33:04 +00:00
|
|
|
);
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 13:43:21 +00:00
|
|
|
fn check_expr<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, bindings: &mut Vec<(Symbol, Span)>) {
|
2018-07-24 06:55:38 +00:00
|
|
|
if in_external_macro(cx.sess(), expr.span) {
|
2016-01-04 04:26:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-09-27 15:16:06 +00:00
|
|
|
match expr.kind {
|
2019-11-25 14:20:10 +00:00
|
|
|
ExprKind::Unary(_, ref e)
|
|
|
|
| ExprKind::Field(ref e, _)
|
2019-11-27 22:34:32 +00:00
|
|
|
| ExprKind::AddrOf(_, _, ref e)
|
2019-11-25 14:20:10 +00:00
|
|
|
| ExprKind::Box(ref e) => check_expr(cx, e, bindings),
|
2021-01-21 01:15:08 +00:00
|
|
|
ExprKind::Block(ref block, _) | ExprKind::Loop(ref block, ..) => check_block(cx, block, bindings),
|
2018-07-12 07:30:57 +00:00
|
|
|
// ExprKind::Call
|
|
|
|
// ExprKind::MethodCall
|
2019-12-27 07:12:26 +00:00
|
|
|
ExprKind::Array(v) | ExprKind::Tup(v) => {
|
2018-11-27 20:14:15 +00:00
|
|
|
for e in v {
|
|
|
|
check_expr(cx, e, bindings)
|
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2021-01-01 18:38:11 +00:00
|
|
|
ExprKind::If(ref cond, ref then, ref otherwise) => {
|
|
|
|
check_expr(cx, cond, bindings);
|
|
|
|
check_expr(cx, &**then, bindings);
|
|
|
|
if let Some(ref o) = *otherwise {
|
|
|
|
check_expr(cx, o, bindings);
|
|
|
|
}
|
|
|
|
},
|
2019-12-27 07:12:26 +00:00
|
|
|
ExprKind::Match(ref init, arms, _) => {
|
2015-08-21 15:11:34 +00:00
|
|
|
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 {
|
2019-09-25 19:00:17 +00:00
|
|
|
check_pat(cx, &arm.pat, Some(&**init), arm.pat.span, bindings);
|
|
|
|
// This is ugly, but needed to get the right type
|
|
|
|
if let Some(ref guard) = arm.guard {
|
|
|
|
match guard {
|
|
|
|
Guard::If(if_expr) => check_expr(cx, if_expr, bindings),
|
2020-11-15 21:51:02 +00:00
|
|
|
Guard::IfLet(guard_pat, guard_expr) => {
|
|
|
|
check_pat(cx, guard_pat, Some(*guard_expr), guard_pat.span, bindings);
|
|
|
|
check_expr(cx, guard_expr, bindings);
|
|
|
|
},
|
2015-09-08 09:50:04 +00:00
|
|
|
}
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|
2019-09-25 19:00:17 +00:00
|
|
|
check_expr(cx, &arm.body, bindings);
|
|
|
|
bindings.truncate(len);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 13:43:21 +00:00
|
|
|
fn check_ty<'tcx>(cx: &LateContext<'tcx>, ty: &'tcx Ty<'_>, bindings: &mut Vec<(Symbol, Span)>) {
|
2019-09-27 15:16:06 +00:00
|
|
|
match ty.kind {
|
2018-07-12 08:03:06 +00:00
|
|
|
TyKind::Slice(ref sty) => check_ty(cx, sty, bindings),
|
|
|
|
TyKind::Array(ref fty, ref anon_const) => {
|
2015-08-21 15:11:34 +00:00
|
|
|
check_ty(cx, fty, bindings);
|
2018-12-08 00:56:03 +00:00
|
|
|
check_expr(cx, &cx.tcx.hir().body(anon_const.body).value, bindings);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2018-11-27 20:14:15 +00:00
|
|
|
TyKind::Ptr(MutTy { ty: ref mty, .. }) | TyKind::Rptr(_, MutTy { ty: ref mty, .. }) => {
|
|
|
|
check_ty(cx, mty, bindings)
|
|
|
|
},
|
2019-12-30 04:02:10 +00:00
|
|
|
TyKind::Tup(tup) => {
|
2018-11-27 20:14:15 +00:00
|
|
|
for t in tup {
|
|
|
|
check_ty(cx, t, bindings)
|
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2018-12-08 00:56:03 +00:00
|
|
|
TyKind::Typeof(ref anon_const) => check_expr(cx, &cx.tcx.hir().body(anon_const.body).value, bindings),
|
2015-08-21 15:11:34 +00:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 13:43:21 +00:00
|
|
|
fn is_self_shadow(name: Symbol, expr: &Expr<'_>) -> bool {
|
2019-09-27 15:16:06 +00:00
|
|
|
match expr.kind {
|
2019-11-27 22:34:32 +00:00
|
|
|
ExprKind::Box(ref inner) | ExprKind::AddrOf(_, _, ref inner) => is_self_shadow(name, inner),
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Block(ref block, _) => {
|
2018-11-27 20:14:15 +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
|
|
|
},
|
2020-01-06 16:39:50 +00:00
|
|
|
ExprKind::Unary(op, ref inner) => (UnOp::UnDeref == op) && is_self_shadow(name, inner),
|
2018-07-12 07:30:57 +00:00
|
|
|
ExprKind::Path(QPath::Resolved(_, ref path)) => path_eq_name(name, path),
|
2015-08-21 15:11:34 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 13:43:21 +00:00
|
|
|
fn path_eq_name(name: Symbol, path: &Path<'_>) -> bool {
|
2021-01-15 09:56:44 +00:00
|
|
|
!path.is_global() && path.segments.len() == 1 && path.segments[0].ident.name == name
|
2015-08-21 15:11:34 +00:00
|
|
|
}
|