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