diff --git a/src/overflow_check_conditional.rs b/src/overflow_check_conditional.rs index c89a3dd27327..2dccbfdb26ac 100644 --- a/src/overflow_check_conditional.rs +++ b/src/overflow_check_conditional.rs @@ -1,14 +1,16 @@ +#![allow(cyclomatic_complexity)] use rustc::lint::*; use rustc_front::hir::*; use utils::{span_lint}; -/// **What it does:** This lint finds classic overflow checks. +/// **What it does:** This lint finds classic underflow / overflow checks. /// -/// **Why is this bad?** Most classic C overflow checks will fail in Rust. Users can use functions like `overflowing_*` and `wrapping_*` instead. +/// **Why is this bad?** Most classic C underflow / overflow checks will fail in Rust. Users can use functions like `overflowing_*` and `wrapping_*` instead. /// /// **Known problems:** None. /// /// **Example:** `a + b < a` + declare_lint!(pub OVERFLOW_CHECK_CONDITIONAL, Warn, "Using overflow checks which are likely to panic"); @@ -22,35 +24,50 @@ impl LintPass for OverflowCheckConditional { } impl LateLintPass for OverflowCheckConditional { + // a + b < a, a > a + b, a < a - b, a - b > a fn check_expr(&mut self, cx: &LateContext, expr: &Expr) { if_let_chain! {[ let Expr_::ExprBinary(ref op, ref first, ref second) = expr.node, - let BinOp_::BiLt = op.node, - let Expr_::ExprBinary(ref op2, ref add1, ref add2) = first.node, - let BinOp_::BiAdd = op2.node, - let Expr_::ExprPath(_,ref path1) = add1.node, - let Expr_::ExprPath(_, ref path2) = add2.node, + let Expr_::ExprBinary(ref op2, ref ident1, ref ident2) = first.node, + let Expr_::ExprPath(_,ref path1) = ident1.node, + let Expr_::ExprPath(_, ref path2) = ident2.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(add1).is_integral(), - cx.tcx.expr_ty(add2).is_integral() + cx.tcx.expr_ty(ident1).is_integral(), + cx.tcx.expr_ty(ident2).is_integral() ], { - span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C overflow conditons that will fail in Rust."); + if let BinOp_::BiLt = op.node { + if let BinOp_::BiAdd = op2.node { + span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C overflow conditons that will fail in Rust."); + } + } + if let BinOp_::BiGt = op.node { + if let BinOp_::BiSub = op2.node { + span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C underflow 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, + let Expr_::ExprBinary(ref op2, ref ident1, ref ident2) = second.node, + let Expr_::ExprPath(_,ref path1) = ident1.node, + let Expr_::ExprPath(_, ref path2) = ident2.node, + let Expr_::ExprPath(_, ref path3) = first.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() + cx.tcx.expr_ty(ident1).is_integral(), + cx.tcx.expr_ty(ident2).is_integral() ], { - span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C underflow conditons that will fail in Rust."); + if let BinOp_::BiGt = op.node { + if let BinOp_::BiAdd = op2.node { + span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C overflow conditons that will fail in Rust."); + } + } + if let BinOp_::BiLt = op.node { + if let BinOp_::BiSub = op2.node { + 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 a59fa2a444a5..df629146deec 100644 --- a/tests/compile-fail/overflow_check_conditional.rs +++ b/tests/compile-fail/overflow_check_conditional.rs @@ -9,21 +9,39 @@ fn main() { let c: u32 = 3; if a + b < a { //~ERROR You are trying to use classic C overflow conditons that will fail in Rust. + } + if a > a + 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 overflow conditons that will fail in Rust. + } + if b > a + 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 b < a - 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 < a - b { //~ERROR You are trying to use classic C underflow conditons that will fail in Rust. + } if a + b < c { + } + if c > a + b { + } if a - b < c { + } + if c > a - b { + } let i = 1.1; let j = 2.2; @@ -32,6 +50,12 @@ fn main() { } if i - j < i { + } + if i > i + j { + + } + if i - j < i { + } }