]> git.lizzy.rs Git - rust.git/blob - src/arithmetic.rs
Don't panic if cargo rustc fails
[rust.git] / src / arithmetic.rs
1 use rustc::hir;
2 use rustc::lint::*;
3 use syntax::codemap::Span;
4 use utils::span_lint;
5
6 /// **What it does:** This lint checks for plain integer arithmetic
7 ///
8 /// **Why is this bad?** This is only checked against overflow in debug builds.
9 /// In some applications one wants explicitly checked, wrapping or saturating
10 /// arithmetic.
11 ///
12 /// **Known problems:** None
13 ///
14 /// **Example:**
15 /// ```
16 /// a + 1
17 /// ```
18 declare_restriction_lint! {
19     pub INTEGER_ARITHMETIC,
20     "Any integer arithmetic statement"
21 }
22
23 /// **What it does:** This lint checks for float arithmetic
24 ///
25 /// **Why is this bad?** For some embedded systems or kernel development, it
26 /// can be useful to rule out floating-point numbers
27 ///
28 /// **Known problems:** None
29 ///
30 /// **Example:**
31 /// ```
32 /// a + 1.0
33 /// ```
34 declare_restriction_lint! {
35     pub FLOAT_ARITHMETIC,
36     "Any floating-point arithmetic statement"
37 }
38
39 #[derive(Copy, Clone, Default)]
40 pub struct Arithmetic {
41     span: Option<Span>
42 }
43
44 impl LintPass for Arithmetic {
45     fn get_lints(&self) -> LintArray {
46         lint_array!(INTEGER_ARITHMETIC, FLOAT_ARITHMETIC)
47     }
48 }
49
50 impl LateLintPass for Arithmetic {
51     fn check_expr(&mut self, cx: &LateContext, expr: &hir::Expr) {
52         if let Some(_) = self.span { return; }
53         match expr.node {
54             hir::ExprBinary(ref op, ref l, ref r) => {
55                 match op.node {
56                     hir::BiAnd | hir::BiOr | hir::BiBitAnd |
57                     hir::BiBitOr | hir::BiBitXor | hir::BiShl | hir::BiShr |
58                     hir::BiEq | hir::BiLt | hir::BiLe | hir::BiNe | hir::BiGe |
59                     hir::BiGt => return,
60                     _ => ()
61                 }
62                 let (l_ty, r_ty) = (cx.tcx.expr_ty(l), cx.tcx.expr_ty(r));
63                 if l_ty.is_integral() && r_ty.is_integral() {
64                     span_lint(cx,
65                               INTEGER_ARITHMETIC,
66                               expr.span,
67                               "integer arithmetic detected");
68                     self.span = Some(expr.span);
69                 } else if l_ty.is_floating_point() && r_ty.is_floating_point() {
70                     span_lint(cx,
71                               FLOAT_ARITHMETIC,
72                               expr.span,
73                               "floating-point arithmetic detected");
74                     self.span = Some(expr.span);
75                 }
76             },
77             hir::ExprUnary(hir::UnOp::UnNeg, ref arg) => {
78                 let ty = cx.tcx.expr_ty(arg);
79                 if ty.is_integral() {
80                     span_lint(cx,
81                               INTEGER_ARITHMETIC,
82                               expr.span,
83                               "integer arithmetic detected");
84                     self.span = Some(expr.span);
85                 } else if ty.is_floating_point() {
86                     span_lint(cx,
87                               FLOAT_ARITHMETIC,
88                               expr.span,
89                               "floating-point arithmetic detected");
90                     self.span = Some(expr.span);
91                 }
92             },
93             _ => ()
94         }
95     }
96
97     fn check_expr_post(&mut self, _: &LateContext, expr: &hir::Expr) {
98         if Some(expr.span) == self.span {
99             self.span = None;
100         }
101     }
102 }