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