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