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