mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-26 14:40:32 +00:00
Merge pull request #940 from oli-obk/simplify/mut_mut
simplify `mut_mut` lint
This commit is contained in:
commit
77ed899941
1 changed files with 23 additions and 41 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
},
|
||||
|_| {
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue