Skip to content

Commit 77ed899

Browse files
committed
Merge pull request #940 from oli-obk/simplify/mut_mut
simplify `mut_mut` lint
2 parents d71e030 + e90a0be commit 77ed899

File tree

1 file changed

+23
-41
lines changed

1 file changed

+23
-41
lines changed

src/mut_mut.rs

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -28,50 +28,32 @@ impl LintPass for MutMut {
2828

2929
impl LateLintPass for MutMut {
3030
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
31-
check_expr_mut(cx, expr)
32-
}
33-
34-
fn check_ty(&mut self, cx: &LateContext, ty: &Ty) {
35-
unwrap_mut(ty).and_then(unwrap_mut).map_or((), |_| {
36-
span_lint(cx, MUT_MUT, ty.span, "generally you want to avoid `&mut &mut _` if possible");
37-
});
38-
}
39-
}
40-
41-
fn check_expr_mut(cx: &LateContext, expr: &Expr) {
42-
fn unwrap_addr(expr: &Expr) -> Option<&Expr> {
43-
match expr.node {
44-
ExprAddrOf(MutMutable, ref e) => Some(e),
45-
_ => None,
31+
if in_external_macro(cx, expr.span) {
32+
return;
4633
}
47-
}
4834

49-
if in_external_macro(cx, expr.span) {
50-
return;
35+
if let ExprAddrOf(MutMutable, ref e) = expr.node {
36+
if let ExprAddrOf(MutMutable, _) = e.node {
37+
span_lint(cx,
38+
MUT_MUT,
39+
expr.span,
40+
"generally you want to avoid `&mut &mut _` if possible");
41+
} else {
42+
if let TyRef(_, TypeAndMut { mutbl: MutMutable, .. }) = cx.tcx.expr_ty(e).sty {
43+
span_lint(cx,
44+
MUT_MUT,
45+
expr.span,
46+
"this expression mutably borrows a mutable reference. Consider reborrowing");
47+
}
48+
}
49+
}
5150
}
5251

53-
unwrap_addr(expr).map_or((), |e| {
54-
unwrap_addr(e).map_or_else(|| {
55-
if let TyRef(_, TypeAndMut { mutbl: MutMutable, .. }) = cx.tcx.expr_ty(e).sty {
56-
span_lint(cx,
57-
MUT_MUT,
58-
expr.span,
59-
"this expression mutably borrows a mutable reference. Consider \
60-
reborrowing");
61-
}
62-
},
63-
|_| {
64-
span_lint(cx,
65-
MUT_MUT,
66-
expr.span,
67-
"generally you want to avoid `&mut &mut _` if possible");
68-
})
69-
})
70-
}
71-
72-
fn unwrap_mut(ty: &Ty) -> Option<&Ty> {
73-
match ty.node {
74-
TyRptr(_, MutTy { ty: ref pty, mutbl: MutMutable }) => Some(pty),
75-
_ => None,
52+
fn check_ty(&mut self, cx: &LateContext, ty: &Ty) {
53+
if let TyRptr(_, MutTy { ty: ref pty, mutbl: MutMutable }) = ty.node {
54+
if let TyRptr(_, MutTy { mutbl: MutMutable, .. }) = pty.node {
55+
span_lint(cx, MUT_MUT, ty.span, "generally you want to avoid `&mut &mut _` if possible");
56+
}
57+
}
7658
}
7759
}

0 commit comments

Comments
 (0)