]> git.lizzy.rs Git - rust.git/commitdiff
Merge branch 'pr-78'
authorManish Goregaokar <manishsmail@gmail.com>
Tue, 11 Aug 2015 17:58:06 +0000 (23:28 +0530)
committerManish Goregaokar <manishsmail@gmail.com>
Tue, 11 Aug 2015 17:58:06 +0000 (23:28 +0530)
Conflicts:
src/lib.rs

src/lib.rs
src/misc.rs
tests/compile-fail/modulo_one.rs [new file with mode: 0644]

index 7d29d96637d4f0a2dc66c5af68e43e193c91a0f0..80ba04031fab34bcb401eb568592acf579ec6c47 100755 (executable)
@@ -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,
                                            ]);
 }
index b3a7419455212f97bf0e3c0e0e4986b26816732e..53bf73de35a7d45ac81c0aa3a276d44a0ca51f9e 100644 (file)
@@ -337,3 +337,34 @@ fn check_fn(&mut self, cx: &Context, _: FnKind, _: &FnDecl,
         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
+}
diff --git a/tests/compile-fail/modulo_one.rs b/tests/compile-fail/modulo_one.rs
new file mode 100644 (file)
index 0000000..26c7de8
--- /dev/null
@@ -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;
+}