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