Merge branch 'pr-78'

Conflicts:
	src/lib.rs
This commit is contained in:
Manish Goregaokar 2015-08-11 23:28:06 +05:30
commit 2cb26126d3
3 changed files with 41 additions and 0 deletions

View file

@ -51,6 +51,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
reg.register_lint_pass(box misc::CmpOwned as LintPassObject);
reg.register_lint_pass(box attrs::AttrPass as LintPassObject);
reg.register_lint_pass(box collapsible_if::CollapsibleIf as LintPassObject);
reg.register_lint_pass(box misc::ModuloOne as LintPassObject);
reg.register_lint_pass(box unicode::Unicode as LintPassObject);
reg.register_lint_pass(box strings::StringAdd as LintPassObject);
reg.register_lint_pass(box misc::NeedlessReturn as LintPassObject);
@ -75,5 +76,6 @@ pub fn plugin_registrar(reg: &mut Registry) {
unicode::ZERO_WIDTH_SPACE,
strings::STRING_ADD_ASSIGN,
misc::NEEDLESS_RETURN,
misc::MODULO_ONE,
]);
}

View file

@ -337,3 +337,34 @@ impl LintPass for NeedlessReturn {
self.check_block_return(cx, block);
}
}
declare_lint!(pub MODULO_ONE, Warn, "Warn on expressions that include % 1, which is always 0");
#[derive(Copy,Clone)]
pub struct ModuloOne;
impl LintPass for ModuloOne {
fn get_lints(&self) -> LintArray {
lint_array!(MODULO_ONE)
}
fn check_expr(&mut self, cx: &Context, expr: &Expr) {
if let ExprBinary(ref cmp, _, ref right) = expr.node {
if let &Spanned {node: BinOp_::BiRem, ..} = cmp {
if is_lit_one(right) {
cx.span_lint(MODULO_ONE, expr.span, "Any number modulo 1 will be 0");
}
}
}
}
}
fn is_lit_one(expr: &Expr) -> bool {
if let ExprLit(ref spanned) = expr.node {
if let LitInt(1, _) = spanned.node {
return true;
}
}
false
}

View file

@ -0,0 +1,8 @@
#![feature(plugin)]
#![plugin(clippy)]
#![deny(modulo_one)]
fn main() {
10 % 1; //~ERROR Any number modulo 1 will be 0
10 % 2;
}