From e90a0be923906ded7c79d6168a11b8b68d85fd72 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Tue, 17 May 2016 16:34:15 +0200 Subject: [PATCH] simplify `mut_mut` lint --- src/mut_mut.rs | 64 ++++++++++++++++++-------------------------------- 1 file changed, 23 insertions(+), 41 deletions(-) diff --git a/src/mut_mut.rs b/src/mut_mut.rs index 7b7b5ecdf4e..4147e288c4f 100644 --- a/src/mut_mut.rs +++ b/src/mut_mut.rs @@ -28,50 +28,32 @@ fn get_lints(&self) -> LintArray { impl LateLintPass for MutMut { fn check_expr(&mut self, cx: &LateContext, expr: &Expr) { - check_expr_mut(cx, expr) - } + if in_external_macro(cx, expr.span) { + return; + } - fn check_ty(&mut self, cx: &LateContext, ty: &Ty) { - unwrap_mut(ty).and_then(unwrap_mut).map_or((), |_| { - span_lint(cx, MUT_MUT, ty.span, "generally you want to avoid `&mut &mut _` if possible"); - }); - } -} - -fn check_expr_mut(cx: &LateContext, expr: &Expr) { - fn unwrap_addr(expr: &Expr) -> Option<&Expr> { - match expr.node { - ExprAddrOf(MutMutable, ref e) => Some(e), - _ => None, + if let ExprAddrOf(MutMutable, ref e) = expr.node { + if let ExprAddrOf(MutMutable, _) = e.node { + span_lint(cx, + MUT_MUT, + expr.span, + "generally you want to avoid `&mut &mut _` if possible"); + } else { + if let TyRef(_, TypeAndMut { mutbl: MutMutable, .. }) = cx.tcx.expr_ty(e).sty { + span_lint(cx, + MUT_MUT, + expr.span, + "this expression mutably borrows a mutable reference. Consider reborrowing"); + } + } } } - if in_external_macro(cx, expr.span) { - return; - } - - unwrap_addr(expr).map_or((), |e| { - unwrap_addr(e).map_or_else(|| { - if let TyRef(_, TypeAndMut { mutbl: MutMutable, .. }) = cx.tcx.expr_ty(e).sty { - span_lint(cx, - MUT_MUT, - expr.span, - "this expression mutably borrows a mutable reference. Consider \ - reborrowing"); - } - }, - |_| { - span_lint(cx, - MUT_MUT, - expr.span, - "generally you want to avoid `&mut &mut _` if possible"); - }) - }) -} - -fn unwrap_mut(ty: &Ty) -> Option<&Ty> { - match ty.node { - TyRptr(_, MutTy { ty: ref pty, mutbl: MutMutable }) => Some(pty), - _ => None, + fn check_ty(&mut self, cx: &LateContext, ty: &Ty) { + if let TyRptr(_, MutTy { ty: ref pty, mutbl: MutMutable }) = ty.node { + if let TyRptr(_, MutTy { mutbl: MutMutable, .. }) = pty.node { + span_lint(cx, MUT_MUT, ty.span, "generally you want to avoid `&mut &mut _` if possible"); + } + } } }