]> git.lizzy.rs Git - rust.git/blobdiff - src/misc.rs
Merge branch 'pr-78'
[rust.git] / src / misc.rs
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
+}