1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
use reexport::*;
use rustc::lint::*;
use rustc::hir::*;
use rustc::hir::intravisit::{Visitor, FnKind, NestedVisitorMap};
use rustc::ty;
use syntax::codemap::Span;
use utils::{higher, in_external_macro, snippet, span_lint_and_then, iter_input_pats};

/// **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
/// let x = &x;
/// ```
declare_lint! {
    pub SHADOW_SAME,
    Allow,
    "rebinding a name to itself, e.g. `let mut x = &mut x`"
}

/// **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
/// let x = x + 1;
/// ```
declare_lint! {
    pub SHADOW_REUSE,
    Allow,
    "rebinding a name to an expression that re-uses the original value, e.g. \
     `let x = x + 1`"
}

/// **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 ore introducing more scopes to contain the bindings.
///
/// **Known problems:** This lint, as the other shadowing related lints,
/// currently only catches very simple patterns.
///
/// **Example:**
/// ```rust
/// let x = y; let x = z; // shadows the earlier binding
/// ```
declare_lint! {
    pub SHADOW_UNRELATED,
    Allow,
    "rebinding a name without even using the original value"
}

#[derive(Copy, Clone)]
pub struct Pass;

impl LintPass for Pass {
    fn get_lints(&self) -> LintArray {
        lint_array!(SHADOW_SAME, SHADOW_REUSE, SHADOW_UNRELATED)
    }
}

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
    fn check_fn(
        &mut self,
        cx: &LateContext<'a, 'tcx>,
        _: FnKind<'tcx>,
        decl: &'tcx FnDecl,
        body: &'tcx Body,
        _: Span,
        _: NodeId
    ) {
        if in_external_macro(cx, body.value.span) {
            return;
        }
        check_fn(cx, decl, body);
    }
}

fn check_fn<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, decl: &'tcx FnDecl, body: &'tcx Body) {
    let mut bindings = Vec::new();
    for arg in iter_input_pats(decl, body) {
        if let PatKind::Binding(_, _, ident, _) = arg.pat.node {
            bindings.push((ident.node, ident.span))
        }
    }
    check_expr(cx, &body.value, &mut bindings);
}

fn check_block<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, block: &'tcx Block, bindings: &mut Vec<(Name, Span)>) {
    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, _) => check_expr(cx, e, bindings),
        }
    }
    if let Some(ref o) = block.expr {
        check_expr(cx, o, bindings);
    }
    bindings.truncate(len);
}

fn check_decl<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, decl: &'tcx Decl, bindings: &mut Vec<(Name, Span)>) {
    if in_external_macro(cx, decl.span) {
        return;
    }
    if higher::is_from_for_desugar(decl) {
        return;
    }
    if let DeclLocal(ref local) = decl.node {
        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);
        }
    }
}

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,
        _ => true,
    }
}

fn check_pat<'a, 'tcx>(
    cx: &LateContext<'a, 'tcx>,
    pat: &'tcx Pat,
    init: Option<&'tcx Expr>,
    span: Span,
    bindings: &mut Vec<(Name, Span)>
) {
    // TODO: match more stuff / destructuring
    match pat.node {
        PatKind::Binding(_, _, ref ident, ref inner) => {
            let name = ident.node;
            if is_binding(cx, pat.id) {
                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));
                }
            }
            if let Some(ref p) = *inner {
                check_pat(cx, p, init, span, bindings);
            }
        },
        PatKind::Struct(_, ref pfields, _) => {
            if let Some(init_struct) = init {
                if let ExprStruct(_, ref efields, _) = init_struct.node {
                    for field in pfields {
                        let name = field.node.name;
                        let efield = efields.iter()
                            .find(|f| f.name.node == name)
                            .map(|f| &*f.expr);
                        check_pat(cx, &field.node.pat, efield, span, bindings);
                    }
                } else {
                    for field in pfields {
                        check_pat(cx, &field.node.pat, init, span, bindings);
                    }
                }
            } else {
                for field in pfields {
                    check_pat(cx, &field.node.pat, None, span, bindings);
                }
            }
        },
        PatKind::Tuple(ref inner, _) => {
            if let Some(init_tup) = init {
                if let ExprTup(ref tup) = init_tup.node {
                    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);
                    }
                }
            } else {
                for p in inner {
                    check_pat(cx, p, None, span, bindings);
                }
            }
        },
        PatKind::Box(ref inner) => {
            if let Some(initp) = init {
                if let ExprBox(ref inner_init) = initp.node {
                    check_pat(cx, inner, Some(&**inner_init), span, bindings);
                } else {
                    check_pat(cx, inner, init, span, bindings);
                }
            } else {
                check_pat(cx, inner, init, span, bindings);
            }
        },
        PatKind::Ref(ref inner, _) => check_pat(cx, inner, init, span, bindings),
        // PatVec(Vec<P<Pat>>, Option<P<Pat>>, Vec<P<Pat>>),
        _ => (),
    }
}

fn lint_shadow<'a, 'tcx: 'a>(
    cx: &LateContext<'a, 'tcx>,
    name: Name,
    span: Span,
    pattern_span: Span,
    init: Option<&'tcx Expr>,
    prev_span: Span
) {
    if let Some(expr) = init {
        if is_self_shadow(name, expr) {
            span_lint_and_then(cx,
                               SHADOW_SAME,
                               span,
                               &format!("`{}` is shadowed by itself in `{}`",
                                        snippet(cx, pattern_span, "_"),
                                        snippet(cx, expr.span, "..")),
                               |db| {
                db.span_note(prev_span, "previous binding is here");
            });
        } else if contains_self(cx, name, expr) {
            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| {
                db.span_note(expr.span, "initialization happens here");
                db.span_note(prev_span, "previous binding is here");
            });
        } else {
            span_lint_and_then(cx,
                               SHADOW_UNRELATED,
                               pattern_span,
                               &format!("`{}` is shadowed by `{}`",
                                        snippet(cx, pattern_span, "_"),
                                        snippet(cx, expr.span, "..")),
                               |db| {
                db.span_note(expr.span, "initialization happens here");
                db.span_note(prev_span, "previous binding is here");
            });
        }

    } else {
        span_lint_and_then(cx,
                           SHADOW_UNRELATED,
                           span,
                           &format!("`{}` shadows a previous declaration", snippet(cx, pattern_span, "_")),
                           |db| {
            db.span_note(prev_span, "previous binding is here");
        });
    }
}

fn check_expr<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, bindings: &mut Vec<(Name, Span)>) {
    if in_external_macro(cx, expr.span) {
        return;
    }
    match expr.node {
        ExprUnary(_, ref e) |
        ExprField(ref e, _) |
        ExprTupField(ref e, _) |
        ExprAddrOf(_, ref e) |
        ExprBox(ref e) => check_expr(cx, e, bindings),
        ExprBlock(ref block) |
        ExprLoop(ref block, _, _) => check_block(cx, block, bindings),
        // ExprCall
        // ExprMethodCall
        ExprArray(ref v) | ExprTup(ref v) => {
            for 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);
        },
        ExprMatch(ref init, ref arms, _) => {
            check_expr(cx, init, bindings);
            let len = bindings.len();
            for arm in arms {
                for pat in &arm.pats {
                    check_pat(cx, pat, Some(&**init), pat.span, bindings);
                    // 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);
                }
            }
        },
        _ => (),
    }
}

fn check_ty<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: &'tcx Ty, bindings: &mut Vec<(Name, Span)>) {
    match ty.node {
        TyObjectSum(ref sty, _) |
        TySlice(ref sty) => check_ty(cx, sty, bindings),
        TyArray(ref fty, body_id) => {
            check_ty(cx, fty, bindings);
            check_expr(cx, &cx.tcx.map.body(body_id).value, bindings);
        },
        TyPtr(MutTy { ty: ref mty, .. }) |
        TyRptr(_, MutTy { ty: ref mty, .. }) => check_ty(cx, mty, bindings),
        TyTup(ref tup) => {
            for t in tup {
                check_ty(cx, t, bindings)
            }
        },
        TyTypeof(body_id) => check_expr(cx, &cx.tcx.map.body(body_id).value, 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, |e| is_self_shadow(name, e))
        },
        ExprUnary(op, ref inner) => (UnDeref == op) && is_self_shadow(name, inner),
        ExprPath(QPath::Resolved(_, ref path)) => path_eq_name(name, path),
        _ => false,
    }
}

fn path_eq_name(name: Name, path: &Path) -> bool {
    !path.is_global() && path.segments.len() == 1 && path.segments[0].name.as_str() == name.as_str()
}

struct ContainsSelf<'a, 'tcx: 'a> {
    name: Name,
    result: bool,
    cx: &'a LateContext<'a, 'tcx>,
}

impl<'a, 'tcx: 'a> Visitor<'tcx> for ContainsSelf<'a, 'tcx> {
    fn visit_name(&mut self, _: Span, name: Name) {
        if self.name == name {
            self.result = true;
        }
    }
    fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
        NestedVisitorMap::All(&self.cx.tcx.map)
    }
}

fn contains_self<'a, 'tcx: 'a>(cx: &LateContext<'a, 'tcx>, name: Name, expr: &'tcx Expr) -> bool {
    let mut cs = ContainsSelf {
        name: name,
        result: false,
        cx: cx,
    };
    cs.visit_expr(expr);
    cs.result
}