]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/mut_mut.rs
update to the rust-PR that unblocks clippy
[rust.git] / clippy_lints / src / mut_mut.rs
1 use rustc::hir;
2 use rustc::hir::intravisit;
3 use rustc::lint::*;
4 use rustc::ty::{TypeAndMut, TyRef};
5 use utils::{higher, in_external_macro, span_lint};
6
7 /// **What it does:** Checks for instances of `mut mut` references.
8 ///
9 /// **Why is this bad?** Multiple `mut`s don't add anything meaningful to the
10 /// source. This is either a copy'n'paste error, or it shows a fundamental
11 /// misunderstanding of references.
12 ///
13 /// **Known problems:** None.
14 ///
15 /// **Example:**
16 /// ```rust
17 /// let x = &mut &mut y;
18 /// ```
19 declare_lint! {
20     pub MUT_MUT,
21     Allow,
22     "usage of double-mut refs, e.g. `&mut &mut ...`"
23 }
24
25 #[derive(Copy,Clone)]
26 pub struct MutMut;
27
28 impl LintPass for MutMut {
29     fn get_lints(&self) -> LintArray {
30         lint_array!(MUT_MUT)
31     }
32 }
33
34 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MutMut {
35     fn check_block(&mut self, cx: &LateContext<'a, 'tcx>, block: &'tcx hir::Block) {
36         intravisit::walk_block(&mut MutVisitor { cx: cx }, block);
37     }
38
39     fn check_ty(&mut self, cx: &LateContext<'a, 'tcx>, ty: &'tcx hir::Ty) {
40         use rustc::hir::intravisit::Visitor;
41
42         MutVisitor { cx: cx }.visit_ty(ty);
43     }
44 }
45
46 pub struct MutVisitor<'a, 'tcx: 'a> {
47     cx: &'a LateContext<'a, 'tcx>,
48 }
49
50 impl<'a, 'tcx> intravisit::Visitor<'tcx> for MutVisitor<'a, 'tcx> {
51     fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
52         if in_external_macro(self.cx, expr.span) {
53             return;
54         }
55
56         if let Some((_, arg, body)) = higher::for_loop(expr) {
57             // A `for` loop lowers to:
58             // ```rust
59             // match ::std::iter::Iterator::next(&mut iter) {
60             // //                                ^^^^
61             // ```
62             // Let's ignore the generated code.
63             intravisit::walk_expr(self, arg);
64             intravisit::walk_expr(self, body);
65         } else if let hir::ExprAddrOf(hir::MutMutable, ref e) = expr.node {
66             if let hir::ExprAddrOf(hir::MutMutable, _) = e.node {
67                 span_lint(self.cx, MUT_MUT, expr.span, "generally you want to avoid `&mut &mut _` if possible");
68             } else if let TyRef(_, TypeAndMut { mutbl: hir::MutMutable, .. }) = self.cx.tcx.tables().expr_ty(e).sty {
69                 span_lint(self.cx,
70                           MUT_MUT,
71                           expr.span,
72                           "this expression mutably borrows a mutable reference. Consider reborrowing");
73             }
74         }
75     }
76
77     fn visit_ty(&mut self, ty: &'tcx hir::Ty) {
78         if let hir::TyRptr(_, hir::MutTy { ty: ref pty, mutbl: hir::MutMutable }) = ty.node {
79             if let hir::TyRptr(_, hir::MutTy { mutbl: hir::MutMutable, .. }) = pty.node {
80                 span_lint(self.cx, MUT_MUT, ty.span, "generally you want to avoid `&mut &mut _` if possible");
81             }
82
83         }
84
85         intravisit::walk_ty(self, ty);
86     }
87     fn nested_visit_map<'this>(&'this mut self) -> intravisit::NestedVisitorMap<'this, 'tcx> {
88         intravisit::NestedVisitorMap::All(&self.cx.tcx.map)
89     }
90 }