]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/mut_mut.rs
Fix lines that exceed max width manually
[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;
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(
68                     self.cx,
69                     MUT_MUT,
70                     expr.span,
71                     "generally you want to avoid `&mut &mut _` if possible",
72                 );
73             } else if let ty::TyRef(
74                 _,
75                 ty::TypeAndMut {
76                     mutbl: hir::MutMutable,
77                     ..
78                 },
79             ) = self.cx.tables.expr_ty(e).sty
80             {
81                 span_lint(
82                     self.cx,
83                     MUT_MUT,
84                     expr.span,
85                     "this expression mutably borrows a mutable reference. Consider reborrowing",
86                 );
87             }
88         }
89     }
90
91     fn visit_ty(&mut self, ty: &'tcx hir::Ty) {
92         if let hir::TyRptr(
93             _,
94             hir::MutTy {
95                 ty: ref pty,
96                 mutbl: hir::MutMutable,
97             },
98         ) = ty.node
99         {
100             if let hir::TyRptr(
101                 _,
102                 hir::MutTy {
103                     mutbl: hir::MutMutable,
104                     ..
105                 },
106             ) = pty.node
107             {
108                 span_lint(
109                     self.cx,
110                     MUT_MUT,
111                     ty.span,
112                     "generally you want to avoid `&mut &mut _` if possible",
113                 );
114             }
115         }
116
117         intravisit::walk_ty(self, ty);
118     }
119     fn nested_visit_map<'this>(&'this mut self) -> intravisit::NestedVisitorMap<'this, 'tcx> {
120         intravisit::NestedVisitorMap::None
121     }
122 }