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