2015-08-16 06:54:43 +00:00
|
|
|
use rustc::lint::*;
|
2016-03-27 18:59:02 +00:00
|
|
|
use rustc::ty::{TypeAndMut, TyRef};
|
2016-04-07 15:46:48 +00:00
|
|
|
use rustc::hir::*;
|
2015-09-06 08:53:55 +00:00
|
|
|
use utils::{in_external_macro, span_lint};
|
2015-05-18 07:02:24 +00:00
|
|
|
|
2016-02-05 23:41:54 +00:00
|
|
|
/// **What it does:** This lint checks for instances of `mut mut` references.
|
2015-12-11 00:22:27 +00:00
|
|
|
///
|
|
|
|
/// **Why is this bad?** Multiple `mut`s don't add anything meaningful to the source.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None
|
|
|
|
///
|
|
|
|
/// **Example:** `let x = &mut &mut y;`
|
2016-02-05 23:13:29 +00:00
|
|
|
declare_lint! {
|
|
|
|
pub MUT_MUT,
|
|
|
|
Allow,
|
|
|
|
"usage of double-mut refs, e.g. `&mut &mut ...` (either copy'n'paste error, \
|
|
|
|
or shows a fundamental misunderstanding of references)"
|
|
|
|
}
|
2015-05-18 07:02:24 +00:00
|
|
|
|
|
|
|
#[derive(Copy,Clone)]
|
|
|
|
pub struct MutMut;
|
|
|
|
|
|
|
|
impl LintPass for MutMut {
|
2015-08-11 18:22:20 +00:00
|
|
|
fn get_lints(&self) -> LintArray {
|
2015-05-18 07:02:24 +00:00
|
|
|
lint_array!(MUT_MUT)
|
2015-08-11 18:22:20 +00:00
|
|
|
}
|
2015-09-19 02:53:04 +00:00
|
|
|
}
|
2015-08-11 18:22:20 +00:00
|
|
|
|
2015-09-19 02:53:04 +00:00
|
|
|
impl LateLintPass for MutMut {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
|
2015-11-17 05:22:57 +00:00
|
|
|
check_expr_mut(cx, expr)
|
2015-08-11 18:22:20 +00:00
|
|
|
}
|
|
|
|
|
2015-09-19 02:53:04 +00:00
|
|
|
fn check_ty(&mut self, cx: &LateContext, ty: &Ty) {
|
2016-01-04 04:26:12 +00:00
|
|
|
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");
|
|
|
|
});
|
2015-08-11 18:22:20 +00:00
|
|
|
}
|
2015-05-18 07:02:24 +00:00
|
|
|
}
|
2015-05-18 08:41:15 +00:00
|
|
|
|
2015-09-19 02:53:04 +00:00
|
|
|
fn check_expr_mut(cx: &LateContext, expr: &Expr) {
|
2015-11-17 05:22:57 +00:00
|
|
|
fn unwrap_addr(expr: &Expr) -> Option<&Expr> {
|
2015-08-11 18:22:20 +00:00
|
|
|
match expr.node {
|
2015-11-27 15:47:24 +00:00
|
|
|
ExprAddrOf(MutMutable, ref e) => Some(e),
|
2016-01-04 04:26:12 +00:00
|
|
|
_ => None,
|
2015-08-11 18:22:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-24 09:16:56 +00:00
|
|
|
if in_external_macro(cx, expr.span) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-11 18:22:20 +00:00
|
|
|
unwrap_addr(expr).map_or((), |e| {
|
2016-01-04 04:26:12 +00:00
|
|
|
unwrap_addr(e).map_or_else(|| {
|
2016-04-14 18:14:03 +00:00
|
|
|
if let TyRef(_, TypeAndMut { mutbl: MutMutable, .. }) = cx.tcx.expr_ty(e).sty {
|
2016-01-04 04:26:12 +00:00
|
|
|
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");
|
|
|
|
})
|
2015-08-11 18:22:20 +00:00
|
|
|
})
|
2015-05-25 05:22:41 +00:00
|
|
|
}
|
|
|
|
|
2015-11-17 05:22:57 +00:00
|
|
|
fn unwrap_mut(ty: &Ty) -> Option<&Ty> {
|
2015-08-11 18:22:20 +00:00
|
|
|
match ty.node {
|
2016-04-14 18:14:03 +00:00
|
|
|
TyRptr(_, MutTy { ty: ref pty, mutbl: MutMutable }) => Some(pty),
|
2016-01-04 04:26:12 +00:00
|
|
|
_ => None,
|
2015-08-11 18:22:20 +00:00
|
|
|
}
|
2015-05-18 08:41:15 +00:00
|
|
|
}
|