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