New lint: mut_mut (closes issue #9)
This commit is contained in:
parent
cd62ef3450
commit
96bfade4f1
4 changed files with 75 additions and 0 deletions
|
|
@ -23,6 +23,7 @@ pub mod needless_bool;
|
|||
pub mod approx_const;
|
||||
pub mod eta_reduction;
|
||||
pub mod identity_op;
|
||||
pub mod mut_mut;
|
||||
|
||||
#[plugin_registrar]
|
||||
pub fn plugin_registrar(reg: &mut Registry) {
|
||||
|
|
@ -40,6 +41,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
|
|||
reg.register_lint_pass(box misc::Precedence as LintPassObject);
|
||||
reg.register_lint_pass(box eta_reduction::EtaPass as LintPassObject);
|
||||
reg.register_lint_pass(box identity_op::IdentityOp as LintPassObject);
|
||||
reg.register_lint_pass(box mut_mut::MutMut as LintPassObject);
|
||||
|
||||
reg.register_lint_group("clippy", vec![types::BOX_VEC, types::LINKEDLIST,
|
||||
misc::SINGLE_MATCH, misc::STR_TO_STRING,
|
||||
|
|
@ -53,5 +55,6 @@ pub fn plugin_registrar(reg: &mut Registry) {
|
|||
misc::PRECEDENCE,
|
||||
eta_reduction::REDUNDANT_CLOSURE,
|
||||
identity_op::IDENTITY_OP,
|
||||
mut_mut::MUT_MUT,
|
||||
]);
|
||||
}
|
||||
|
|
|
|||
51
src/mut_mut.rs
Normal file
51
src/mut_mut.rs
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
use syntax::ptr::P;
|
||||
use syntax::ast;
|
||||
use syntax::ast::*;
|
||||
use syntax::ast_util::{is_comparison_binop, binop_to_string};
|
||||
use syntax::visit::{FnKind};
|
||||
use rustc::lint::{Context, LintPass, LintArray, Lint, Level};
|
||||
use rustc::middle::ty::{self, expr_ty, ty_str, ty_ptr, ty_rptr, ty_float};
|
||||
use syntax::codemap::{Span, Spanned};
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
if unwrap_addr(expr).and_then(unwrap_addr).is_some() {
|
||||
cx.span_lint(MUT_MUT, expr.span,
|
||||
"We're not sure what this means, so if you know, please tell us.")
|
||||
}
|
||||
}
|
||||
|
||||
fn check_ty(&mut self, cx: &Context, ty: &Ty) {
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
if unwrap_mut(ty).and_then(unwrap_mut).is_some() {
|
||||
cx.span_lint(MUT_MUT, ty.span,
|
||||
"We're not sure what this means, so if you know, please tell us.")
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue