simplify mut_mut lint

This commit is contained in:
Oliver Schneider 2016-05-17 16:34:15 +02:00
parent d71e030cd7
commit e90a0be923
No known key found for this signature in database
GPG Key ID: 56D6EEA0FC67AC46

View File

@ -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");
}
}
}
}