Merge pull request #940 from oli-obk/simplify/mut_mut

simplify `mut_mut` lint
This commit is contained in:
Martin Carton 2016-05-25 13:07:59 +02:00
commit 77ed899941

View file

@ -28,50 +28,32 @@ impl LintPass for MutMut {
impl LateLintPass for MutMut { impl LateLintPass for MutMut {
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) { 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) { if let ExprAddrOf(MutMutable, ref e) = expr.node {
unwrap_mut(ty).and_then(unwrap_mut).map_or((), |_| { if let ExprAddrOf(MutMutable, _) = e.node {
span_lint(cx, MUT_MUT, ty.span, "generally you want to avoid `&mut &mut _` if possible"); span_lint(cx,
}); MUT_MUT,
} expr.span,
} "generally you want to avoid `&mut &mut _` if possible");
} else {
fn check_expr_mut(cx: &LateContext, expr: &Expr) { if let TyRef(_, TypeAndMut { mutbl: MutMutable, .. }) = cx.tcx.expr_ty(e).sty {
fn unwrap_addr(expr: &Expr) -> Option<&Expr> { span_lint(cx,
match expr.node { MUT_MUT,
ExprAddrOf(MutMutable, ref e) => Some(e), expr.span,
_ => None, "this expression mutably borrows a mutable reference. Consider reborrowing");
}
}
} }
} }
if in_external_macro(cx, expr.span) { fn check_ty(&mut self, cx: &LateContext, ty: &Ty) {
return; 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");
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,
} }
} }