]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/overflow_check_conditional.rs
split clippy into lints, plugin and cargo-clippy
[rust.git] / clippy_lints / src / overflow_check_conditional.rs
1 use rustc::lint::*;
2 use rustc::hir::*;
3 use utils::span_lint;
4
5 /// **What it does:** This lint finds classic underflow / overflow checks.
6 ///
7 /// **Why is this bad?** Most classic C underflow / overflow checks will fail in Rust. Users can use functions like `overflowing_*` and `wrapping_*` instead.
8 ///
9 /// **Known problems:** None.
10 ///
11 /// **Example:** `a + b < a`
12
13 declare_lint!(pub OVERFLOW_CHECK_CONDITIONAL, Warn,
14               "Using overflow checks which are likely to panic");
15
16 #[derive(Copy, Clone)]
17 pub struct OverflowCheckConditional;
18
19 impl LintPass for OverflowCheckConditional {
20     fn get_lints(&self) -> LintArray {
21         lint_array!(OVERFLOW_CHECK_CONDITIONAL)
22     }
23 }
24
25 impl LateLintPass for OverflowCheckConditional {
26     // a + b < a, a > a + b, a < a - b, a - b > a
27     fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
28         if_let_chain! {[
29         let Expr_::ExprBinary(ref op, ref first, ref second) = expr.node,
30         let Expr_::ExprBinary(ref op2, ref ident1, ref ident2) = first.node,
31         let Expr_::ExprPath(_,ref path1) = ident1.node,
32         let Expr_::ExprPath(_, ref path2) = ident2.node,
33         let Expr_::ExprPath(_, ref path3) = second.node,
34         &path1.segments[0] == &path3.segments[0] || &path2.segments[0] == &path3.segments[0],
35         cx.tcx.expr_ty(ident1).is_integral(),
36         cx.tcx.expr_ty(ident2).is_integral()
37         ], {
38             if let BinOp_::BiLt = op.node {
39                 if let BinOp_::BiAdd = op2.node {
40                     span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C overflow conditions that will fail in Rust.");
41                 }
42             }
43             if let BinOp_::BiGt = op.node {
44                 if let BinOp_::BiSub = op2.node {
45                     span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C underflow conditions that will fail in Rust.");
46                 }
47             }
48         }}
49
50         if_let_chain! {[
51         let Expr_::ExprBinary(ref op, ref first, ref second) = expr.node,
52         let Expr_::ExprBinary(ref op2, ref ident1, ref ident2) = second.node,
53         let Expr_::ExprPath(_,ref path1) = ident1.node,
54         let Expr_::ExprPath(_, ref path2) = ident2.node,
55         let Expr_::ExprPath(_, ref path3) = first.node,
56         &path1.segments[0] == &path3.segments[0] || &path2.segments[0] == &path3.segments[0],
57         cx.tcx.expr_ty(ident1).is_integral(),
58         cx.tcx.expr_ty(ident2).is_integral()
59         ], {
60             if let BinOp_::BiGt = op.node {
61                 if let BinOp_::BiAdd = op2.node {
62                     span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C overflow conditions that will fail in Rust.");
63                 }
64             }
65             if let BinOp_::BiLt = op.node {
66                 if let BinOp_::BiSub = op2.node {
67                     span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, "You are trying to use classic C underflow conditions that will fail in Rust.");
68                 }
69             }
70         }}
71     }
72 }