From d6d409414ea82866bac7f4c7698c16f9884a1b9b Mon Sep 17 00:00:00 2001 From: KALPESH KRISHNA Date: Tue, 8 Mar 2016 02:57:45 +0530 Subject: [PATCH] Adding underflow checks and tests --- src/overflow_check_conditional.rs | 15 +++++++++++++++ tests/compile-fail/overflow_check_conditional.rs | 12 ++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/overflow_check_conditional.rs b/src/overflow_check_conditional.rs index 7f4f4b359..c89a3dd27 100644 --- a/src/overflow_check_conditional.rs +++ b/src/overflow_check_conditional.rs @@ -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."); + }} } } diff --git a/tests/compile-fail/overflow_check_conditional.rs b/tests/compile-fail/overflow_check_conditional.rs index 0b5e486df..a59fa2a44 100644 --- a/tests/compile-fail/overflow_check_conditional.rs +++ b/tests/compile-fail/overflow_check_conditional.rs @@ -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 { + } }