]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/overflow_check_conditional.rs
Auto merge of #3946 - rchaser53:issue-3920, r=flip1995
[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_tool_lint, lint_array};
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 #[derive(Copy, Clone)]
25 pub struct OverflowCheckConditional;
26
27 impl LintPass for OverflowCheckConditional {
28     fn get_lints(&self) -> LintArray {
29         lint_array!(OVERFLOW_CHECK_CONDITIONAL)
30     }
31
32     fn name(&self) -> &'static str {
33         "OverflowCheckConditional"
34     }
35 }
36
37 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OverflowCheckConditional {
38     // a + b < a, a > a + b, a < a - b, a - b > a
39     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
40         let eq = |l, r| SpanlessEq::new(cx).eq_path_segment(l, r);
41         if_chain! {
42             if let ExprKind::Binary(ref op, ref first, ref second) = expr.node;
43             if let ExprKind::Binary(ref op2, ref ident1, ref ident2) = first.node;
44             if let ExprKind::Path(QPath::Resolved(_, ref path1)) = ident1.node;
45             if let ExprKind::Path(QPath::Resolved(_, ref path2)) = ident2.node;
46             if let ExprKind::Path(QPath::Resolved(_, ref path3)) = second.node;
47             if eq(&path1.segments[0], &path3.segments[0]) || eq(&path2.segments[0], &path3.segments[0]);
48             if cx.tables.expr_ty(ident1).is_integral();
49             if cx.tables.expr_ty(ident2).is_integral();
50             then {
51                 if let BinOpKind::Lt = op.node {
52                     if let BinOpKind::Add = op2.node {
53                         span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
54                             "You are trying to use classic C overflow conditions that will fail in Rust.");
55                     }
56                 }
57                 if let BinOpKind::Gt = op.node {
58                     if let BinOpKind::Sub = op2.node {
59                         span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
60                             "You are trying to use classic C underflow conditions that will fail in Rust.");
61                     }
62                 }
63             }
64         }
65
66         if_chain! {
67             if let ExprKind::Binary(ref op, ref first, ref second) = expr.node;
68             if let ExprKind::Binary(ref op2, ref ident1, ref ident2) = second.node;
69             if let ExprKind::Path(QPath::Resolved(_, ref path1)) = ident1.node;
70             if let ExprKind::Path(QPath::Resolved(_, ref path2)) = ident2.node;
71             if let ExprKind::Path(QPath::Resolved(_, ref path3)) = first.node;
72             if eq(&path1.segments[0], &path3.segments[0]) || eq(&path2.segments[0], &path3.segments[0]);
73             if cx.tables.expr_ty(ident1).is_integral();
74             if cx.tables.expr_ty(ident2).is_integral();
75             then {
76                 if let BinOpKind::Gt = op.node {
77                     if let BinOpKind::Add = op2.node {
78                         span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
79                             "You are trying to use classic C overflow conditions that will fail in Rust.");
80                     }
81                 }
82                 if let BinOpKind::Lt = op.node {
83                     if let BinOpKind::Sub = op2.node {
84                         span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
85                             "You are trying to use classic C underflow conditions that will fail in Rust.");
86                     }
87                 }
88             }
89         }
90     }
91 }