Skip to content

simplify mut_mut lint #940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 23 additions & 41 deletions src/mut_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,50 +28,32 @@ impl LintPass for MutMut {

impl LateLintPass for MutMut {
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
check_expr_mut(cx, expr)
}

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 in_external_macro(cx, expr.span) {
return;
}
}

if in_external_macro(cx, expr.span) {
return;
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");
}
}
}
}

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