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