]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/arithmetic.rs
Auto merge of #3946 - rchaser53:issue-3920, r=flip1995
[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 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 LintPass for Arithmetic {
52     fn get_lints(&self) -> LintArray {
53         lint_array!(INTEGER_ARITHMETIC, FLOAT_ARITHMETIC)
54     }
55
56     fn name(&self) -> &'static str {
57         "Arithmetic"
58     }
59 }
60
61 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Arithmetic {
62     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
63         if self.expr_span.is_some() {
64             return;
65         }
66
67         if let Some(span) = self.const_span {
68             if span.contains(expr.span) {
69                 return;
70             }
71         }
72         match &expr.node {
73             hir::ExprKind::Binary(op, l, r) => {
74                 match op.node {
75                     hir::BinOpKind::And
76                     | hir::BinOpKind::Or
77                     | hir::BinOpKind::BitAnd
78                     | hir::BinOpKind::BitOr
79                     | hir::BinOpKind::BitXor
80                     | hir::BinOpKind::Shl
81                     | hir::BinOpKind::Shr
82                     | hir::BinOpKind::Eq
83                     | hir::BinOpKind::Lt
84                     | hir::BinOpKind::Le
85                     | hir::BinOpKind::Ne
86                     | hir::BinOpKind::Ge
87                     | hir::BinOpKind::Gt => return,
88                     _ => (),
89                 }
90                 let (l_ty, r_ty) = (cx.tables.expr_ty(l), cx.tables.expr_ty(r));
91                 if l_ty.is_integral() && r_ty.is_integral() {
92                     span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
93                     self.expr_span = Some(expr.span);
94                 } else if l_ty.is_floating_point() && r_ty.is_floating_point() {
95                     span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
96                     self.expr_span = Some(expr.span);
97                 }
98             },
99             hir::ExprKind::Unary(hir::UnOp::UnNeg, arg) => {
100                 let ty = cx.tables.expr_ty(arg);
101                 if ty.is_integral() {
102                     if constant_simple(cx, cx.tables, expr).is_none() {
103                         span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
104                         self.expr_span = Some(expr.span);
105                     }
106                 } else if ty.is_floating_point() {
107                     span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
108                     self.expr_span = Some(expr.span);
109                 }
110             },
111             _ => (),
112         }
113     }
114
115     fn check_expr_post(&mut self, _: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
116         if Some(expr.span) == self.expr_span {
117             self.expr_span = None;
118         }
119     }
120
121     fn check_body(&mut self, cx: &LateContext<'_, '_>, body: &hir::Body) {
122         let body_owner = cx.tcx.hir().body_owner(body.id());
123
124         match cx.tcx.hir().body_owner_kind(body_owner) {
125             hir::BodyOwnerKind::Static(_) | hir::BodyOwnerKind::Const => {
126                 let body_span = cx.tcx.hir().span(body_owner);
127
128                 if let Some(span) = self.const_span {
129                     if span.contains(body_span) {
130                         return;
131                     }
132                 }
133                 self.const_span = Some(body_span);
134             },
135             hir::BodyOwnerKind::Fn | hir::BodyOwnerKind::Closure => (),
136         }
137     }
138
139     fn check_body_post(&mut self, cx: &LateContext<'_, '_>, body: &hir::Body) {
140         let body_owner = cx.tcx.hir().body_owner(body.id());
141         let body_span = cx.tcx.hir().span(body_owner);
142
143         if let Some(span) = self.const_span {
144             if span.contains(body_span) {
145                 return;
146             }
147         }
148         self.const_span = None;
149     }
150 }