2 use rustc_front::hir::*;
3 use rustc::middle::ty::{TypeAndMut, TyRef};
5 use utils::{in_external_macro, span_lint};
7 /// **What it does:** This lint checks for instances of `mut mut` references. It is `Warn` by default.
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;`
14 declare_lint!(pub MUT_MUT, Allow,
15 "usage of double-mut refs, e.g. `&mut &mut ...` (either copy'n'paste error, \
16 or shows a fundamental misunderstanding of references)");
21 impl LintPass for MutMut {
22 fn get_lints(&self) -> LintArray {
27 impl LateLintPass for MutMut {
28 fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
29 check_expr_mut(cx, expr)
32 fn check_ty(&mut self, cx: &LateContext, ty: &Ty) {
33 unwrap_mut(ty).and_then(unwrap_mut).map_or((), |_| span_lint(cx, MUT_MUT,
34 ty.span, "generally you want to avoid `&mut &mut _` if possible"))
38 fn check_expr_mut(cx: &LateContext, expr: &Expr) {
39 if in_external_macro(cx, expr.span) { return; }
41 fn unwrap_addr(expr: &Expr) -> Option<&Expr> {
43 ExprAddrOf(MutMutable, ref e) => Some(e),
48 unwrap_addr(expr).map_or((), |e| {
49 unwrap_addr(e).map_or_else(
51 if let TyRef(_, TypeAndMut{mutbl: MutMutable, ..}) =
52 cx.tcx.expr_ty(e).sty {
53 span_lint(cx, MUT_MUT, expr.span,
54 "this expression mutably borrows a mutable reference. \
55 Consider reborrowing")
59 span_lint(cx, MUT_MUT, expr.span,
60 "generally you want to avoid `&mut &mut _` if possible")
66 fn unwrap_mut(ty: &Ty) -> Option<&Ty> {
68 TyRptr(_, MutTy{ ty: ref pty, mutbl: MutMutable }) => Some(pty),