]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/mut_mut.rs
Merge branch 'master' into sugg
[rust.git] / clippy_lints / src / mut_mut.rs
1 use rustc::hir;
2 use rustc::hir::intravisit;
3 use rustc::lint::*;
4 use rustc::ty::{TypeAndMut, TyRef};
5 use utils::{higher, in_external_macro, span_lint};
6
7 /// **What it does:** This lint checks for instances of `mut mut` references.
8 ///
9 /// **Why is this bad?** Multiple `mut`s don't add anything meaningful to the source.
10 ///
11 /// **Known problems:** None
12 ///
13 /// **Example:** `let x = &mut &mut y;`
14 declare_lint! {
15     pub MUT_MUT,
16     Allow,
17     "usage of double-mut refs, e.g. `&mut &mut ...` (either copy'n'paste error, \
18      or shows a fundamental misunderstanding of references)"
19 }
20
21 #[derive(Copy,Clone)]
22 pub struct MutMut;
23
24 impl LintPass for MutMut {
25     fn get_lints(&self) -> LintArray {
26         lint_array!(MUT_MUT)
27     }
28 }
29
30 impl LateLintPass for MutMut {
31     fn check_block(&mut self, cx: &LateContext, block: &hir::Block) {
32         intravisit::walk_block(&mut MutVisitor { cx: cx }, block);
33     }
34
35     fn check_ty(&mut self, cx: &LateContext, ty: &hir::Ty) {
36         use rustc::hir::intravisit::Visitor;
37
38         MutVisitor { cx: cx }.visit_ty(ty);
39     }
40 }
41
42 pub struct MutVisitor<'a, 'tcx: 'a> {
43     cx: &'a LateContext<'a, 'tcx>,
44 }
45
46 impl<'a, 'tcx, 'v> intravisit::Visitor<'v> for MutVisitor<'a, 'tcx> {
47     fn visit_expr(&mut self, expr: &'v hir::Expr) {
48         if in_external_macro(self.cx, 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::ExprAddrOf(hir::MutMutable, ref e) = expr.node {
62             if let hir::ExprAddrOf(hir::MutMutable, _) = e.node {
63                 span_lint(self.cx, MUT_MUT, expr.span, "generally you want to avoid `&mut &mut _` if possible");
64             } else if let TyRef(_, TypeAndMut { mutbl: hir::MutMutable, .. }) = self.cx.tcx.expr_ty(e).sty {
65                 span_lint(self.cx,
66                           MUT_MUT,
67                           expr.span,
68                           "this expression mutably borrows a mutable reference. Consider reborrowing");
69             }
70         }
71     }
72
73     fn visit_ty(&mut self, ty: &hir::Ty) {
74         if let hir::TyRptr(_, hir::MutTy { ty: ref pty, mutbl: hir::MutMutable }) = ty.node {
75             if let hir::TyRptr(_, hir::MutTy { mutbl: hir::MutMutable, .. }) = pty.node {
76                 span_lint(self.cx, MUT_MUT, ty.span, "generally you want to avoid `&mut &mut _` if possible");
77             }
78
79         }
80
81         intravisit::walk_ty(self, ty);
82     }
83 }