rust-clippy/src/mut_mut.rs

57 lines
1.5 KiB
Rust
Raw Normal View History

2015-05-18 07:02:24 +00:00
use syntax::ptr::P;
use syntax::ast::*;
use rustc::lint::{Context, LintPass, LintArray, Lint};
use rustc::middle::ty::{expr_ty, sty, ty_ptr, ty_rptr, mt};
2015-05-18 07:02:24 +00:00
declare_lint!(pub MUT_MUT, Warn,
"Warn on usage of double-mut refs, e.g. '&mut &mut ...'");
#[derive(Copy,Clone)]
pub struct MutMut;
impl LintPass for MutMut {
fn get_lints(&self) -> LintArray {
lint_array!(MUT_MUT)
}
fn check_expr(&mut self, cx: &Context, expr: &Expr) {
fn unwrap_addr(expr : &Expr) -> Option<&Expr> {
match expr.node {
ExprAddrOf(MutMutable, ref e) => Option::Some(e),
_ => Option::None
}
}
unwrap_addr(expr).map(|e| {
if unwrap_addr(e).is_some() {
cx.span_lint(MUT_MUT, expr.span,
2015-05-18 08:52:43 +00:00
"Generally you want to avoid &mut &mut _ if possible.")
} else {
match expr_ty(cx.tcx, e).sty {
ty_ptr(mt{ty: _, mutbl: MutMutable}) |
ty_rptr(_, mt{ty: _, mutbl: MutMutable}) =>
cx.span_lint(MUT_MUT, expr.span,
2015-05-18 08:52:43 +00:00
"This expression mutably borrows a mutable reference. Consider reborrowing"),
_ => ()
}
}
});
2015-05-18 07:02:24 +00:00
}
fn check_ty(&mut self, cx: &Context, ty: &Ty) {
if unwrap_mut(ty).and_then(unwrap_mut).is_some() {
cx.span_lint(MUT_MUT, ty.span,
2015-05-18 08:52:43 +00:00
"Generally you want to avoid &mut &mut _ if possible.")
2015-05-18 07:02:24 +00:00
}
}
}
fn unwrap_mut(ty : &Ty) -> Option<&Ty> {
match ty.node {
TyPtr(MutTy{ ty: ref pty, mutbl: MutMutable }) => Option::Some(pty),
TyRptr(_, MutTy{ ty: ref pty, mutbl: MutMutable }) => Option::Some(pty),
_ => Option::None
}
}