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