|
| 1 | +#![allow(cyclomatic_complexity)] |
| 2 | +use rustc::lint::*; |
| 3 | +use rustc_front::hir::*; |
| 4 | +use utils::{span_lint}; |
| 5 | + |
| 6 | +/// **What it does:** This lint finds classic underflow / overflow checks. |
| 7 | +/// |
| 8 | +/// **Why is this bad?** Most classic C underflow / overflow checks will fail in Rust. Users can use functions like `overflowing_*` and `wrapping_*` instead. |
| 9 | +/// |
| 10 | +/// **Known problems:** None. |
| 11 | +/// |
| 12 | +/// **Example:** `a + b < a` |
| 13 | +
|
| 14 | +declare_lint!(pub OVERFLOW_CHECK_CONDITIONAL, Warn, |
| 15 | + "Using overflow checks which are likely to panic"); |
| 16 | + |
| 17 | +#[derive(Copy, Clone)] |
| 18 | +pub struct OverflowCheckConditional; |
| 19 | + |
| 20 | +impl LintPass for OverflowCheckConditional { |
| 21 | + fn get_lints(&self) -> LintArray { |
| 22 | + lint_array!(OVERFLOW_CHECK_CONDITIONAL) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +impl LateLintPass for OverflowCheckConditional { |
| 27 | + // a + b < a, a > a + b, a < a - b, a - b > a |
| 28 | + fn check_expr(&mut self, cx: &LateContext, expr: &Expr) { |
| 29 | + if_let_chain! {[ |
| 30 | + let Expr_::ExprBinary(ref op, ref first, ref second) = expr.node, |
| 31 | + let Expr_::ExprBinary(ref op2, ref ident1, ref ident2) = first.node, |
| 32 | + let Expr_::ExprPath(_,ref path1) = ident1.node, |
| 33 | + let Expr_::ExprPath(_, ref path2) = ident2.node, |
| 34 | + let Expr_::ExprPath(_, ref path3) = second.node, |
| 35 | + (&path1.segments[0]).identifier == (&path3.segments[0]).identifier || (&path2.segments[0]).identifier == (&path3.segments[0]).identifier, |
| 36 | + cx.tcx.expr_ty(ident1).is_integral(), |
| 37 | + cx.tcx.expr_ty(ident2).is_integral() |
| 38 | + ], { |
| 39 | + if let BinOp_::BiLt = op.node { |
| 40 | + if let BinOp_::BiAdd = op2.node { |
| 41 | + span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C overflow conditons that will fail in Rust."); |
| 42 | + } |
| 43 | + } |
| 44 | + if let BinOp_::BiGt = op.node { |
| 45 | + if let BinOp_::BiSub = op2.node { |
| 46 | + span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C underflow conditons that will fail in Rust."); |
| 47 | + } |
| 48 | + } |
| 49 | + }} |
| 50 | + |
| 51 | + if_let_chain! {[ |
| 52 | + let Expr_::ExprBinary(ref op, ref first, ref second) = expr.node, |
| 53 | + let Expr_::ExprBinary(ref op2, ref ident1, ref ident2) = second.node, |
| 54 | + let Expr_::ExprPath(_,ref path1) = ident1.node, |
| 55 | + let Expr_::ExprPath(_, ref path2) = ident2.node, |
| 56 | + let Expr_::ExprPath(_, ref path3) = first.node, |
| 57 | + (&path1.segments[0]).identifier == (&path3.segments[0]).identifier || (&path2.segments[0]).identifier == (&path3.segments[0]).identifier, |
| 58 | + cx.tcx.expr_ty(ident1).is_integral(), |
| 59 | + cx.tcx.expr_ty(ident2).is_integral() |
| 60 | + ], { |
| 61 | + if let BinOp_::BiGt = op.node { |
| 62 | + if let BinOp_::BiAdd = op2.node { |
| 63 | + span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C overflow conditons that will fail in Rust."); |
| 64 | + } |
| 65 | + } |
| 66 | + if let BinOp_::BiLt = op.node { |
| 67 | + if let BinOp_::BiSub = op2.node { |
| 68 | + span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C underflow conditons that will fail in Rust."); |
| 69 | + } |
| 70 | + } |
| 71 | + }} |
| 72 | + } |
| 73 | +} |
0 commit comments