]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/arithmetic.rs
Auto merge of #3677 - daxpedda:integer_arithmetic, r=oli-obk
[rust.git] / clippy_lints / src / arithmetic.rs
1 use crate::consts::constant_simple;
2 use crate::utils::span_lint;
3 use rustc::hir;
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use rustc::{declare_tool_lint, lint_array};
6 use syntax::source_map::Span;
7
8 /// **What it does:** Checks for plain integer arithmetic.
9 ///
10 /// **Why is this bad?** This is only checked against overflow in debug builds.
11 /// In some applications one wants explicitly checked, wrapping or saturating
12 /// arithmetic.
13 ///
14 /// **Known problems:** None.
15 ///
16 /// **Example:**
17 /// ```rust
18 /// a + 1
19 /// ```
20 declare_clippy_lint! {
21     pub INTEGER_ARITHMETIC,
22     restriction,
23     "any integer arithmetic statement"
24 }
25
26 /// **What it does:** Checks for float arithmetic.
27 ///
28 /// **Why is this bad?** For some embedded systems or kernel development, it
29 /// can be useful to rule out floating-point numbers.
30 ///
31 /// **Known problems:** None.
32 ///
33 /// **Example:**
34 /// ```rust
35 /// a + 1.0
36 /// ```
37 declare_clippy_lint! {
38     pub FLOAT_ARITHMETIC,
39     restriction,
40     "any floating-point arithmetic statement"
41 }
42
43 #[derive(Copy, Clone, Default)]
44 pub struct Arithmetic {
45     expr_span: Option<Span>,
46     /// This field is used to check whether expressions are constants, such as in enum discriminants
47     /// and consts
48     const_span: Option<Span>,
49 }
50
51 impl LintPass for Arithmetic {
52     fn get_lints(&self) -> LintArray {
53         lint_array!(INTEGER_ARITHMETIC, FLOAT_ARITHMETIC)
54     }
55 }
56
57 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Arithmetic {
58     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
59         if self.expr_span.is_some() {
60             return;
61         }
62
63         if let Some(span) = self.const_span {
64             if span.contains(expr.span) {
65                 return;
66             }
67         }
68         match &expr.node {
69             hir::ExprKind::Binary(op, l, r) => {
70                 match op.node {
71                     hir::BinOpKind::And
72                     | hir::BinOpKind::Or
73                     | hir::BinOpKind::BitAnd
74                     | hir::BinOpKind::BitOr
75                     | hir::BinOpKind::BitXor
76                     | hir::BinOpKind::Shl
77                     | hir::BinOpKind::Shr
78                     | hir::BinOpKind::Eq
79                     | hir::BinOpKind::Lt
80                     | hir::BinOpKind::Le
81                     | hir::BinOpKind::Ne
82                     | hir::BinOpKind::Ge
83                     | hir::BinOpKind::Gt => return,
84                     _ => (),
85                 }
86                 let (l_ty, r_ty) = (cx.tables.expr_ty(l), cx.tables.expr_ty(r));
87                 if l_ty.is_integral() && r_ty.is_integral() {
88                     span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
89                     self.expr_span = Some(expr.span);
90                 } else if l_ty.is_floating_point() && r_ty.is_floating_point() {
91                     span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
92                     self.expr_span = Some(expr.span);
93                 }
94             },
95             hir::ExprKind::Unary(hir::UnOp::UnNeg, arg) => {
96                 let ty = cx.tables.expr_ty(arg);
97                 if ty.is_integral() {
98                     if constant_simple(cx, cx.tables, expr).is_none() {
99                         span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
100                         self.expr_span = Some(expr.span);
101                     }
102                 } else if ty.is_floating_point() {
103                     span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
104                     self.expr_span = Some(expr.span);
105                 }
106             },
107             _ => (),
108         }
109     }
110
111     fn check_expr_post(&mut self, _: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
112         if Some(expr.span) == self.expr_span {
113             self.expr_span = None;
114         }
115     }
116
117     fn check_body(&mut self, cx: &LateContext<'_, '_>, body: &hir::Body) {
118         let body_owner = cx.tcx.hir().body_owner(body.id());
119
120         match cx.tcx.hir().body_owner_kind(body_owner) {
121             hir::BodyOwnerKind::Static(_) | hir::BodyOwnerKind::Const => {
122                 let body_span = cx.tcx.hir().span(body_owner);
123
124                 if let Some(span) = self.const_span {
125                     if span.contains(body_span) {
126                         return;
127                     }
128                 }
129                 self.const_span = Some(body_span);
130             },
131             hir::BodyOwnerKind::Fn => (),
132         }
133     }
134
135     fn check_body_post(&mut self, cx: &LateContext<'_, '_>, body: &hir::Body) {
136         let body_owner = cx.tcx.hir().body_owner(body.id());
137         let body_span = cx.tcx.hir().span(body_owner);
138
139         if let Some(span) = self.const_span {
140             if span.contains(body_span) {
141                 return;
142             }
143         }
144         self.const_span = None;
145     }
146 }