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