1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 use crate::reexport::*;
11 use crate::rustc::hir::intravisit::FnKind;
12 use crate::rustc::hir::*;
13 use crate::rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass};
15 use crate::rustc::{declare_tool_lint, lint_array};
16 use crate::syntax::source_map::Span;
17 use crate::utils::{contains_name, higher, iter_input_pats, snippet, span_lint_and_then};
19 /// **What it does:** Checks for bindings that shadow other bindings already in
20 /// scope, while just changing reference level or mutability.
22 /// **Why is this bad?** Not much, in fact it's a very common pattern in Rust
23 /// code. Still, some may opt to avoid it in their code base, they can set this
26 /// **Known problems:** This lint, as the other shadowing related lints,
27 /// currently only catches very simple patterns.
33 declare_clippy_lint! {
36 "rebinding a name to itself, e.g. `let mut x = &mut x`"
39 /// **What it does:** Checks for bindings that shadow other bindings already in
40 /// scope, while reusing the original value.
42 /// **Why is this bad?** Not too much, in fact it's a common pattern in Rust
43 /// code. Still, some argue that name shadowing like this hurts readability,
44 /// because a value may be bound to different things depending on position in
47 /// **Known problems:** This lint, as the other shadowing related lints,
48 /// currently only catches very simple patterns.
54 /// use different variable name:
58 declare_clippy_lint! {
61 "rebinding a name to an expression that re-uses the original value, e.g. `let x = x + 1`"
64 /// **What it does:** Checks for bindings that shadow other bindings already in
65 /// scope, either without a initialization or with one that does not even use
66 /// the original value.
68 /// **Why is this bad?** Name shadowing can hurt readability, especially in
69 /// large code bases, because it is easy to lose track of the active binding at
70 /// any place in the code. This can be alleviated by either giving more specific
71 /// names to bindings or introducing more scopes to contain the bindings.
73 /// **Known problems:** This lint, as the other shadowing related lints,
74 /// currently only catches very simple patterns.
79 /// let x = z; // shadows the earlier binding
81 declare_clippy_lint! {
84 "rebinding a name without even using the original value"
87 #[derive(Copy, Clone)]
90 impl LintPass for Pass {
91 fn get_lints(&self) -> LintArray {
92 lint_array!(SHADOW_SAME, SHADOW_REUSE, SHADOW_UNRELATED)
96 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
99 cx: &LateContext<'a, 'tcx>,
106 if in_external_macro(cx.sess(), body.value.span) {
109 check_fn(cx, decl, body);
113 fn check_fn<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, decl: &'tcx FnDecl, body: &'tcx Body) {
114 let mut bindings = Vec::new();
115 for arg in iter_input_pats(decl, body) {
116 if let PatKind::Binding(_, _, ident, _) = arg.pat.node {
117 bindings.push((ident.name, ident.span))
120 check_expr(cx, &body.value, &mut bindings);
123 fn check_block<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, block: &'tcx Block, bindings: &mut Vec<(Name, Span)>) {
124 let len = bindings.len();
125 for stmt in &block.stmts {
127 StmtKind::Decl(ref decl, _) => check_decl(cx, decl, bindings),
128 StmtKind::Expr(ref e, _) | StmtKind::Semi(ref e, _) => check_expr(cx, e, bindings),
131 if let Some(ref o) = block.expr {
132 check_expr(cx, o, bindings);
134 bindings.truncate(len);
137 fn check_decl<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, decl: &'tcx Decl, bindings: &mut Vec<(Name, Span)>) {
138 if in_external_macro(cx.sess(), decl.span) {
141 if higher::is_from_for_desugar(decl) {
144 if let DeclKind::Local(ref local) = decl.node {
152 if let Some(ref t) = *ty {
153 check_ty(cx, t, bindings)
155 if let Some(ref o) = *init {
156 check_expr(cx, o, bindings);
157 check_pat(cx, pat, Some(o), span, bindings);
159 check_pat(cx, pat, None, span, bindings);
164 fn is_binding(cx: &LateContext<'_, '_>, pat_id: HirId) -> bool {
165 let var_ty = cx.tables.node_id_to_type(pat_id);
167 ty::Adt(..) => false,
172 fn check_pat<'a, 'tcx>(
173 cx: &LateContext<'a, 'tcx>,
175 init: Option<&'tcx Expr>,
177 bindings: &mut Vec<(Name, Span)>,
179 // TODO: match more stuff / destructuring
181 PatKind::Binding(_, _, ident, ref inner) => {
182 let name = ident.name;
183 if is_binding(cx, pat.hir_id) {
184 let mut new_binding = true;
185 for tup in bindings.iter_mut() {
187 lint_shadow(cx, name, span, pat.span, init, tup.1);
194 bindings.push((name, ident.span));
197 if let Some(ref p) = *inner {
198 check_pat(cx, p, init, span, bindings);
201 PatKind::Struct(_, ref pfields, _) => {
202 if let Some(init_struct) = init {
203 if let ExprKind::Struct(_, ref efields, _) = init_struct.node {
204 for field in pfields {
205 let name = field.node.ident.name;
206 let efield = efields.iter().find(|f| f.ident.name == name).map(|f| &*f.expr);
207 check_pat(cx, &field.node.pat, efield, span, bindings);
210 for field in pfields {
211 check_pat(cx, &field.node.pat, init, span, bindings);
215 for field in pfields {
216 check_pat(cx, &field.node.pat, None, span, bindings);
220 PatKind::Tuple(ref inner, _) => {
221 if let Some(init_tup) = init {
222 if let ExprKind::Tup(ref tup) = init_tup.node {
223 for (i, p) in inner.iter().enumerate() {
224 check_pat(cx, p, Some(&tup[i]), p.span, bindings);
228 check_pat(cx, p, init, span, bindings);
233 check_pat(cx, p, None, span, bindings);
237 PatKind::Box(ref inner) => {
238 if let Some(initp) = init {
239 if let ExprKind::Box(ref inner_init) = initp.node {
240 check_pat(cx, inner, Some(&**inner_init), span, bindings);
242 check_pat(cx, inner, init, span, bindings);
245 check_pat(cx, inner, init, span, bindings);
248 PatKind::Ref(ref inner, _) => check_pat(cx, inner, init, span, bindings),
249 // PatVec(Vec<P<Pat>>, Option<P<Pat>>, Vec<P<Pat>>),
254 fn lint_shadow<'a, 'tcx: 'a>(
255 cx: &LateContext<'a, 'tcx>,
259 init: Option<&'tcx Expr>,
262 if let Some(expr) = init {
263 if is_self_shadow(name, expr) {
269 "`{}` is shadowed by itself in `{}`",
270 snippet(cx, pattern_span, "_"),
271 snippet(cx, expr.span, "..")
274 db.span_note(prev_span, "previous binding is here");
277 } else if contains_name(name, expr) {
283 "`{}` is shadowed by `{}` which reuses the original value",
284 snippet(cx, pattern_span, "_"),
285 snippet(cx, expr.span, "..")
288 db.span_note(expr.span, "initialization happens here");
289 db.span_note(prev_span, "previous binding is here");
298 "`{}` is shadowed by `{}`",
299 snippet(cx, pattern_span, "_"),
300 snippet(cx, expr.span, "..")
303 db.span_note(expr.span, "initialization happens here");
304 db.span_note(prev_span, "previous binding is here");
313 &format!("`{}` shadows a previous declaration", snippet(cx, pattern_span, "_")),
315 db.span_note(prev_span, "previous binding is here");
321 fn check_expr<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, bindings: &mut Vec<(Name, Span)>) {
322 if in_external_macro(cx.sess(), expr.span) {
326 ExprKind::Unary(_, ref e) | ExprKind::Field(ref e, _) | ExprKind::AddrOf(_, ref e) | ExprKind::Box(ref e) => {
327 check_expr(cx, e, bindings)
329 ExprKind::Block(ref block, _) | ExprKind::Loop(ref block, _, _) => check_block(cx, block, bindings),
331 // ExprKind::MethodCall
332 ExprKind::Array(ref v) | ExprKind::Tup(ref v) => {
334 check_expr(cx, e, bindings)
337 ExprKind::If(ref cond, ref then, ref otherwise) => {
338 check_expr(cx, cond, bindings);
339 check_expr(cx, &**then, bindings);
340 if let Some(ref o) = *otherwise {
341 check_expr(cx, o, bindings);
344 ExprKind::While(ref cond, ref block, _) => {
345 check_expr(cx, cond, bindings);
346 check_block(cx, block, bindings);
348 ExprKind::Match(ref init, ref arms, _) => {
349 check_expr(cx, init, bindings);
350 let len = bindings.len();
352 for pat in &arm.pats {
353 check_pat(cx, pat, Some(&**init), pat.span, bindings);
354 // This is ugly, but needed to get the right type
355 if let Some(ref guard) = arm.guard {
357 Guard::If(if_expr) => check_expr(cx, if_expr, bindings),
360 check_expr(cx, &arm.body, bindings);
361 bindings.truncate(len);
369 fn check_ty<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: &'tcx Ty, bindings: &mut Vec<(Name, Span)>) {
371 TyKind::Slice(ref sty) => check_ty(cx, sty, bindings),
372 TyKind::Array(ref fty, ref anon_const) => {
373 check_ty(cx, fty, bindings);
374 check_expr(cx, &cx.tcx.hir().body(anon_const.body).value, bindings);
376 TyKind::Ptr(MutTy { ty: ref mty, .. }) | TyKind::Rptr(_, MutTy { ty: ref mty, .. }) => {
377 check_ty(cx, mty, bindings)
379 TyKind::Tup(ref tup) => {
381 check_ty(cx, t, bindings)
384 TyKind::Typeof(ref anon_const) => check_expr(cx, &cx.tcx.hir().body(anon_const.body).value, bindings),
389 fn is_self_shadow(name: Name, expr: &Expr) -> bool {
391 ExprKind::Box(ref inner) | ExprKind::AddrOf(_, ref inner) => is_self_shadow(name, inner),
392 ExprKind::Block(ref block, _) => {
393 block.stmts.is_empty() && block.expr.as_ref().map_or(false, |e| is_self_shadow(name, e))
395 ExprKind::Unary(op, ref inner) => (UnDeref == op) && is_self_shadow(name, inner),
396 ExprKind::Path(QPath::Resolved(_, ref path)) => path_eq_name(name, path),
401 fn path_eq_name(name: Name, path: &Path) -> bool {
402 !path.is_global() && path.segments.len() == 1 && path.segments[0].ident.as_str() == name.as_str()