2 use rustc::ty::{TypeAndMut, TyRef};
4 use utils::{in_external_macro, span_lint};
6 /// **What it does:** This lint checks for instances of `mut mut` references.
8 /// **Why is this bad?** Multiple `mut`s don't add anything meaningful to the source.
10 /// **Known problems:** None
12 /// **Example:** `let x = &mut &mut y;`
16 "usage of double-mut refs, e.g. `&mut &mut ...` (either copy'n'paste error, \
17 or shows a fundamental misunderstanding of references)"
23 impl LintPass for MutMut {
24 fn get_lints(&self) -> LintArray {
29 impl LateLintPass for MutMut {
30 fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
31 check_expr_mut(cx, expr)
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");
41 fn check_expr_mut(cx: &LateContext, expr: &Expr) {
42 fn unwrap_addr(expr: &Expr) -> Option<&Expr> {
44 ExprAddrOf(MutMutable, ref e) => Some(e),
49 if in_external_macro(cx, expr.span) {
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 {
59 "this expression mutably borrows a mutable reference. Consider \
67 "generally you want to avoid `&mut &mut _` if possible");
72 fn unwrap_mut(ty: &Ty) -> Option<&Ty> {
74 TyRptr(_, MutTy { ty: ref pty, mutbl: MutMutable }) => Some(pty),