Skip to content

Commit

Permalink
Adding underflow checks and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
martiansideofthemoon committed Mar 7, 2016
1 parent 55fbf59 commit d6d4094
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/overflow_check_conditional.rs
Expand Up @@ -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.");
}}
}
}
12 changes: 12 additions & 0 deletions tests/compile-fail/overflow_check_conditional.rs
Expand Up @@ -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 {

}
}

0 comments on commit d6d4094

Please sign in to comment.