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