]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/overflow_check_conditional.rs
Merge remote-tracking branch 'origin/beta1.35' into backport-merge
[rust.git] / clippy_lints / src / overflow_check_conditional.rs
1 use crate::utils::{span_lint, SpanlessEq};
2 use if_chain::if_chain;
3 use rustc::hir::*;
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use rustc::{declare_lint_pass, declare_tool_lint};
6
7 declare_clippy_lint! {
8     /// **What it does:** Detects classic underflow/overflow checks.
9     ///
10     /// **Why is this bad?** Most classic C underflow/overflow checks will fail in
11     /// Rust. Users can use functions like `overflowing_*` and `wrapping_*` instead.
12     ///
13     /// **Known problems:** None.
14     ///
15     /// **Example:**
16     /// ```rust
17     /// a + b < a
18     /// ```
19     pub OVERFLOW_CHECK_CONDITIONAL,
20     complexity,
21     "overflow checks inspired by C which are likely to panic"
22 }
23
24 declare_lint_pass!(OverflowCheckConditional => [OVERFLOW_CHECK_CONDITIONAL]);
25
26 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OverflowCheckConditional {
27     // a + b < a, a > a + b, a < a - b, a - b > a
28     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
29         let eq = |l, r| SpanlessEq::new(cx).eq_path_segment(l, r);
30         if_chain! {
31             if let ExprKind::Binary(ref op, ref first, ref second) = expr.node;
32             if let ExprKind::Binary(ref op2, ref ident1, ref ident2) = first.node;
33             if let ExprKind::Path(QPath::Resolved(_, ref path1)) = ident1.node;
34             if let ExprKind::Path(QPath::Resolved(_, ref path2)) = ident2.node;
35             if let ExprKind::Path(QPath::Resolved(_, ref path3)) = second.node;
36             if eq(&path1.segments[0], &path3.segments[0]) || eq(&path2.segments[0], &path3.segments[0]);
37             if cx.tables.expr_ty(ident1).is_integral();
38             if cx.tables.expr_ty(ident2).is_integral();
39             then {
40                 if let BinOpKind::Lt = op.node {
41                     if let BinOpKind::Add = op2.node {
42                         span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
43                             "You are trying to use classic C overflow conditions that will fail in Rust.");
44                     }
45                 }
46                 if let BinOpKind::Gt = op.node {
47                     if let BinOpKind::Sub = op2.node {
48                         span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
49                             "You are trying to use classic C underflow conditions that will fail in Rust.");
50                     }
51                 }
52             }
53         }
54
55         if_chain! {
56             if let ExprKind::Binary(ref op, ref first, ref second) = expr.node;
57             if let ExprKind::Binary(ref op2, ref ident1, ref ident2) = second.node;
58             if let ExprKind::Path(QPath::Resolved(_, ref path1)) = ident1.node;
59             if let ExprKind::Path(QPath::Resolved(_, ref path2)) = ident2.node;
60             if let ExprKind::Path(QPath::Resolved(_, ref path3)) = first.node;
61             if eq(&path1.segments[0], &path3.segments[0]) || eq(&path2.segments[0], &path3.segments[0]);
62             if cx.tables.expr_ty(ident1).is_integral();
63             if cx.tables.expr_ty(ident2).is_integral();
64             then {
65                 if let BinOpKind::Gt = op.node {
66                     if let BinOpKind::Add = op2.node {
67                         span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
68                             "You are trying to use classic C overflow conditions that will fail in Rust.");
69                     }
70                 }
71                 if let BinOpKind::Lt = op.node {
72                     if let BinOpKind::Sub = op2.node {
73                         span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
74                             "You are trying to use classic C underflow conditions that will fail in Rust.");
75                     }
76                 }
77             }
78         }
79     }
80 }