]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/arithmetic.rs
Auto merge of #3596 - xfix:remove-crate-from-paths, r=flip1995
[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 use crate::utils::span_lint;
11 use rustc::hir;
12 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
13 use rustc::{declare_tool_lint, lint_array};
14 use syntax::source_map::Span;
15
16 /// **What it does:** Checks for plain integer arithmetic.
17 ///
18 /// **Why is this bad?** This is only checked against overflow in debug builds.
19 /// In some applications one wants explicitly checked, wrapping or saturating
20 /// arithmetic.
21 ///
22 /// **Known problems:** None.
23 ///
24 /// **Example:**
25 /// ```rust
26 /// a + 1
27 /// ```
28 declare_clippy_lint! {
29     pub INTEGER_ARITHMETIC,
30     restriction,
31     "any integer arithmetic statement"
32 }
33
34 /// **What it does:** Checks for float arithmetic.
35 ///
36 /// **Why is this bad?** For some embedded systems or kernel development, it
37 /// can be useful to rule out floating-point numbers.
38 ///
39 /// **Known problems:** None.
40 ///
41 /// **Example:**
42 /// ```rust
43 /// a + 1.0
44 /// ```
45 declare_clippy_lint! {
46     pub FLOAT_ARITHMETIC,
47     restriction,
48     "any floating-point arithmetic statement"
49 }
50
51 #[derive(Copy, Clone, Default)]
52 pub struct Arithmetic {
53     expr_span: Option<Span>,
54     /// This field is used to check whether expressions are constants, such as in enum discriminants
55     /// 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(_) | hir::BodyOwnerKind::Const => {
128                 let body_span = cx.tcx.hir().span(body_owner);
129
130                 if let Some(span) = self.const_span {
131                     if span.contains(body_span) {
132                         return;
133                     }
134                 }
135                 self.const_span = Some(body_span);
136             },
137             hir::BodyOwnerKind::Fn => (),
138         }
139     }
140
141     fn check_body_post(&mut self, cx: &LateContext<'_, '_>, body: &hir::Body) {
142         let body_owner = cx.tcx.hir().body_owner(body.id());
143         let body_span = cx.tcx.hir().span(body_owner);
144
145         if let Some(span) = self.const_span {
146             if span.contains(body_span) {
147                 return;
148             }
149         }
150         self.const_span = None;
151     }
152 }