]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/mut_mut.rs
Merge pull request #2821 from mati865/rust-2018-migration
[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 crate::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_clippy_lint! {
20     pub MUT_MUT,
21     pedantic,
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 }, 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 }.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                 _,
76                 hir::MutMutable,
77             ) = self.cx.tables.expr_ty(e).sty
78             {
79                 span_lint(
80                     self.cx,
81                     MUT_MUT,
82                     expr.span,
83                     "this expression mutably borrows a mutable reference. Consider reborrowing",
84                 );
85             }
86         }
87     }
88
89     fn visit_ty(&mut self, ty: &'tcx hir::Ty) {
90         if let hir::TyRptr(
91             _,
92             hir::MutTy {
93                 ty: ref pty,
94                 mutbl: hir::MutMutable,
95             },
96         ) = ty.node
97         {
98             if let hir::TyRptr(
99                 _,
100                 hir::MutTy {
101                     mutbl: hir::MutMutable,
102                     ..
103                 },
104             ) = pty.node
105             {
106                 span_lint(
107                     self.cx,
108                     MUT_MUT,
109                     ty.span,
110                     "generally you want to avoid `&mut &mut _` if possible",
111                 );
112             }
113         }
114
115         intravisit::walk_ty(self, ty);
116     }
117     fn nested_visit_map<'this>(&'this mut self) -> intravisit::NestedVisitorMap<'this, 'tcx> {
118         intravisit::NestedVisitorMap::None
119     }
120 }