]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/mut_mut.rs
Rollup merge of #94113 - Mizobrook-kan:issue-94025, r=estebank
[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::lint::in_external_macro;
7 use rustc_middle::ty;
8 use rustc_session::{declare_lint_pass, declare_tool_lint};
9
10 declare_clippy_lint! {
11     /// ### What it does
12     /// Checks for instances of `mut mut` references.
13     ///
14     /// ### Why is this bad?
15     /// Multiple `mut`s don't add anything meaningful to the
16     /// source. This is either a copy'n'paste error, or it shows a fundamental
17     /// misunderstanding of references.
18     ///
19     /// ### Example
20     /// ```rust
21     /// # let mut y = 1;
22     /// let x = &mut &mut y;
23     /// ```
24     #[clippy::version = "pre 1.29.0"]
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     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(higher::ForLoop { arg, body, .. }) = higher::ForLoop::hir(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, 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.typeck_results().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 in_external_macro(self.cx.sess(), ty.span) {
84             return;
85         }
86
87         if let hir::TyKind::Rptr(
88             _,
89             hir::MutTy {
90                 ty: pty,
91                 mutbl: hir::Mutability::Mut,
92             },
93         ) = ty.kind
94         {
95             if let hir::TyKind::Rptr(
96                 _,
97                 hir::MutTy {
98                     mutbl: hir::Mutability::Mut,
99                     ..
100                 },
101             ) = pty.kind
102             {
103                 span_lint(
104                     self.cx,
105                     MUT_MUT,
106                     ty.span,
107                     "generally you want to avoid `&mut &mut _` if possible",
108                 );
109             }
110         }
111
112         intravisit::walk_ty(self, ty);
113     }
114 }