]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/mut_mut.rs
Rustup to rust-lang/rust#66878
[rust.git] / clippy_lints / src / mut_mut.rs
1 use crate::utils::{higher, span_lint};
2 use rustc::declare_lint_pass;
3 use rustc::hir;
4 use rustc::hir::intravisit;
5 use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass};
6 use rustc::ty;
7 use rustc_session::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     fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
48         if in_external_macro(self.cx.sess(), expr.span) {
49             return;
50         }
51
52         if let Some((_, arg, body)) = higher::for_loop(expr) {
53             // A `for` loop lowers to:
54             // ```rust
55             // match ::std::iter::Iterator::next(&mut iter) {
56             // //                                ^^^^
57             // ```
58             // Let's ignore the generated code.
59             intravisit::walk_expr(self, arg);
60             intravisit::walk_expr(self, body);
61         } else if let hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Mutable, ref e) = expr.kind {
62             if let hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Mutable, _) = e.kind {
63                 span_lint(
64                     self.cx,
65                     MUT_MUT,
66                     expr.span,
67                     "generally you want to avoid `&mut &mut _` if possible",
68                 );
69             } else if let ty::Ref(_, _, hir::Mutability::Mutable) = self.cx.tables.expr_ty(e).kind {
70                 span_lint(
71                     self.cx,
72                     MUT_MUT,
73                     expr.span,
74                     "this expression mutably borrows a mutable reference. Consider reborrowing",
75                 );
76             }
77         }
78     }
79
80     fn visit_ty(&mut self, ty: &'tcx hir::Ty) {
81         if let hir::TyKind::Rptr(
82             _,
83             hir::MutTy {
84                 ty: ref pty,
85                 mutbl: hir::Mutability::Mutable,
86             },
87         ) = ty.kind
88         {
89             if let hir::TyKind::Rptr(
90                 _,
91                 hir::MutTy {
92                     mutbl: hir::Mutability::Mutable,
93                     ..
94                 },
95             ) = pty.kind
96             {
97                 span_lint(
98                     self.cx,
99                     MUT_MUT,
100                     ty.span,
101                     "generally you want to avoid `&mut &mut _` if possible",
102                 );
103             }
104         }
105
106         intravisit::walk_ty(self, ty);
107     }
108     fn nested_visit_map<'this>(&'this mut self) -> intravisit::NestedVisitorMap<'this, 'tcx> {
109         intravisit::NestedVisitorMap::None
110     }
111 }