]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/overflow_check_conditional.rs
Auto merge of #3596 - xfix:remove-crate-from-paths, r=flip1995
[rust.git] / clippy_lints / src / overflow_check_conditional.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 use crate::utils::{span_lint, SpanlessEq};
11 use if_chain::if_chain;
12 use rustc::hir::*;
13 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
14 use rustc::{declare_tool_lint, lint_array};
15
16 /// **What it does:** Detects classic underflow/overflow checks.
17 ///
18 /// **Why is this bad?** Most classic C underflow/overflow checks will fail in
19 /// Rust. Users can use functions like `overflowing_*` and `wrapping_*` instead.
20 ///
21 /// **Known problems:** None.
22 ///
23 /// **Example:**
24 /// ```rust
25 /// a + b < a
26 /// ```
27 declare_clippy_lint! {
28     pub OVERFLOW_CHECK_CONDITIONAL,
29     complexity,
30     "overflow checks inspired by C which are likely to panic"
31 }
32
33 #[derive(Copy, Clone)]
34 pub struct OverflowCheckConditional;
35
36 impl LintPass for OverflowCheckConditional {
37     fn get_lints(&self) -> LintArray {
38         lint_array!(OVERFLOW_CHECK_CONDITIONAL)
39     }
40 }
41
42 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OverflowCheckConditional {
43     // a + b < a, a > a + b, a < a - b, a - b > a
44     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
45         let eq = |l, r| SpanlessEq::new(cx).eq_path_segment(l, r);
46         if_chain! {
47             if let ExprKind::Binary(ref op, ref first, ref second) = expr.node;
48             if let ExprKind::Binary(ref op2, ref ident1, ref ident2) = first.node;
49             if let ExprKind::Path(QPath::Resolved(_, ref path1)) = ident1.node;
50             if let ExprKind::Path(QPath::Resolved(_, ref path2)) = ident2.node;
51             if let ExprKind::Path(QPath::Resolved(_, ref path3)) = second.node;
52             if eq(&path1.segments[0], &path3.segments[0]) || eq(&path2.segments[0], &path3.segments[0]);
53             if cx.tables.expr_ty(ident1).is_integral();
54             if cx.tables.expr_ty(ident2).is_integral();
55             then {
56                 if let BinOpKind::Lt = op.node {
57                     if let BinOpKind::Add = op2.node {
58                         span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
59                             "You are trying to use classic C overflow conditions that will fail in Rust.");
60                     }
61                 }
62                 if let BinOpKind::Gt = op.node {
63                     if let BinOpKind::Sub = op2.node {
64                         span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
65                             "You are trying to use classic C underflow conditions that will fail in Rust.");
66                     }
67                 }
68             }
69         }
70
71         if_chain! {
72             if let ExprKind::Binary(ref op, ref first, ref second) = expr.node;
73             if let ExprKind::Binary(ref op2, ref ident1, ref ident2) = second.node;
74             if let ExprKind::Path(QPath::Resolved(_, ref path1)) = ident1.node;
75             if let ExprKind::Path(QPath::Resolved(_, ref path2)) = ident2.node;
76             if let ExprKind::Path(QPath::Resolved(_, ref path3)) = first.node;
77             if eq(&path1.segments[0], &path3.segments[0]) || eq(&path2.segments[0], &path3.segments[0]);
78             if cx.tables.expr_ty(ident1).is_integral();
79             if cx.tables.expr_ty(ident2).is_integral();
80             then {
81                 if let BinOpKind::Gt = op.node {
82                     if let BinOpKind::Add = op2.node {
83                         span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
84                             "You are trying to use classic C overflow conditions that will fail in Rust.");
85                     }
86                 }
87                 if let BinOpKind::Lt = op.node {
88                     if let BinOpKind::Sub = op2.node {
89                         span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
90                             "You are trying to use classic C underflow conditions that will fail in Rust.");
91                     }
92                 }
93             }
94         }
95     }
96 }