2 use rustc::hir::intravisit;
4 use rustc::ty::{TypeAndMut, TyRef};
5 use utils::{higher, in_external_macro, span_lint};
7 /// **What it does:** This lint checks for instances of `mut mut` references.
9 /// **Why is this bad?** Multiple `mut`s don't add anything meaningful to the source.
11 /// **Known problems:** None
13 /// **Example:** `let x = &mut &mut y;`
17 "usage of double-mut refs, e.g. `&mut &mut ...` (either copy'n'paste error, \
18 or shows a fundamental misunderstanding of references)"
24 impl LintPass for MutMut {
25 fn get_lints(&self) -> LintArray {
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);
35 fn check_ty(&mut self, cx: &LateContext, ty: &hir::Ty) {
36 use rustc::hir::intravisit::Visitor;
38 MutVisitor { cx: cx }.visit_ty(ty);
42 pub struct MutVisitor<'a, 'tcx: 'a> {
43 cx: &'a LateContext<'a, 'tcx>,
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) {
52 if let Some((_, arg, body)) = higher::for_loop(expr) {
53 // A `for` loop lowers to:
55 // match ::std::iter::Iterator::next(&mut iter) {
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 {
68 "this expression mutably borrows a mutable reference. Consider reborrowing");
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");
81 intravisit::walk_ty(self, ty);