Adding underflow checks and tests

This commit is contained in:
KALPESH KRISHNA 2016-03-08 02:57:45 +05:30
parent 55fbf59f3b
commit d6d409414e
2 changed files with 27 additions and 0 deletions

View File

@ -37,5 +37,20 @@ impl LateLintPass for OverflowCheckConditional {
], {
span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C overflow conditons that will fail in Rust.");
}}
if_let_chain! {[
let Expr_::ExprBinary(ref op, ref first, ref second) = expr.node,
let BinOp_::BiGt = op.node,
let Expr_::ExprBinary(ref op2, ref sub1, ref sub2) = first.node,
let BinOp_::BiSub = op2.node,
let Expr_::ExprPath(_,ref path1) = sub1.node,
let Expr_::ExprPath(_, ref path2) = sub2.node,
let Expr_::ExprPath(_, ref path3) = second.node,
(&path1.segments[0]).identifier == (&path3.segments[0]).identifier || (&path2.segments[0]).identifier == (&path3.segments[0]).identifier,
cx.tcx.expr_ty(sub1).is_integral(),
cx.tcx.expr_ty(sub2).is_integral()
], {
span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C underflow conditons that will fail in Rust.");
}}
}
}

View File

@ -12,14 +12,26 @@ fn main() {
}
if a + b < b { //~ERROR You are trying to use classic C overflow conditons that will fail in Rust.
}
if a - b > b { //~ERROR You are trying to use classic C underflow conditons that will fail in Rust.
}
if a - b > a { //~ERROR You are trying to use classic C underflow conditons that will fail in Rust.
}
if a + b < c {
}
if a - b < c {
}
let i = 1.1;
let j = 2.2;
if i + j < i {
}
if i - j < i {
}
}