about summary refs log tree commit diff
path: root/clippy_lints/src/overflow_check_conditional.rs
blob: e222782c2cc8c597f2b7755b4098ff88ddd860e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use clippy_utils::diagnostics::span_lint;
use clippy_utils::SpanlessEq;
use if_chain::if_chain;
use rustc_hir::{BinOpKind, Expr, ExprKind, QPath};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};

declare_clippy_lint! {
    /// **What it does:** Detects classic underflow/overflow checks.
    ///
    /// **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:**
    /// ```rust
    /// # let a = 1;
    /// # let b = 2;
    /// a + b < a;
    /// ```
    pub OVERFLOW_CHECK_CONDITIONAL,
    complexity,
    "overflow checks inspired by C which are likely to panic"
}

declare_lint_pass!(OverflowCheckConditional => [OVERFLOW_CHECK_CONDITIONAL]);

impl<'tcx> LateLintPass<'tcx> for OverflowCheckConditional {
    // a + b < a, a > a + b, a < a - b, a - b > a
    fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
        let eq = |l, r| SpanlessEq::new(cx).eq_path_segment(l, r);
        if_chain! {
            if let ExprKind::Binary(ref op, first, second) = expr.kind;
            if let ExprKind::Binary(ref op2, ident1, ident2) = first.kind;
            if let ExprKind::Path(QPath::Resolved(_, path1)) = ident1.kind;
            if let ExprKind::Path(QPath::Resolved(_, path2)) = ident2.kind;
            if let ExprKind::Path(QPath::Resolved(_, path3)) = second.kind;
            if eq(&path1.segments[0], &path3.segments[0]) || eq(&path2.segments[0], &path3.segments[0]);
            if cx.typeck_results().expr_ty(ident1).is_integral();
            if cx.typeck_results().expr_ty(ident2).is_integral();
            then {
                if let BinOpKind::Lt = op.node {
                    if let BinOpKind::Add = op2.node {
                        span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
                            "you are trying to use classic C overflow conditions that will fail in Rust");
                    }
                }
                if let BinOpKind::Gt = op.node {
                    if let BinOpKind::Sub = op2.node {
                        span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
                            "you are trying to use classic C underflow conditions that will fail in Rust");
                    }
                }
            }
        }

        if_chain! {
            if let ExprKind::Binary(ref op, first, second) = expr.kind;
            if let ExprKind::Binary(ref op2, ident1, ident2) = second.kind;
            if let ExprKind::Path(QPath::Resolved(_, path1)) = ident1.kind;
            if let ExprKind::Path(QPath::Resolved(_, path2)) = ident2.kind;
            if let ExprKind::Path(QPath::Resolved(_, path3)) = first.kind;
            if eq(&path1.segments[0], &path3.segments[0]) || eq(&path2.segments[0], &path3.segments[0]);
            if cx.typeck_results().expr_ty(ident1).is_integral();
            if cx.typeck_results().expr_ty(ident2).is_integral();
            then {
                if let BinOpKind::Gt = op.node {
                    if let BinOpKind::Add = op2.node {
                        span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
                            "you are trying to use classic C overflow conditions that will fail in Rust");
                    }
                }
                if let BinOpKind::Lt = op.node {
                    if let BinOpKind::Sub = op2.node {
                        span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
                            "you are trying to use classic C underflow conditions that will fail in Rust");
                    }
                }
            }
        }
    }
}