]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/arithmetic.rs
Merge pull request #3065 from rust-lang-nursery/rustup
[rust.git] / clippy_lints / src / arithmetic.rs
1 use crate::utils::span_lint;
2 use rustc::hir;
3 use rustc::lint::*;
4 use rustc::{declare_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     span: Option<Span>,
45 }
46
47 impl LintPass for Arithmetic {
48     fn get_lints(&self) -> LintArray {
49         lint_array!(INTEGER_ARITHMETIC, FLOAT_ARITHMETIC)
50     }
51 }
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.span.is_some() {
56             return;
57         }
58         match expr.node {
59             hir::ExprKind::Binary(ref op, ref l, ref r) => {
60                 match op.node {
61                     hir::BinOpKind::And
62                     | hir::BinOpKind::Or
63                     | hir::BinOpKind::BitAnd
64                     | hir::BinOpKind::BitOr
65                     | hir::BinOpKind::BitXor
66                     | hir::BinOpKind::Shl
67                     | hir::BinOpKind::Shr
68                     | hir::BinOpKind::Eq
69                     | hir::BinOpKind::Lt
70                     | hir::BinOpKind::Le
71                     | hir::BinOpKind::Ne
72                     | hir::BinOpKind::Ge
73                     | hir::BinOpKind::Gt => return,
74                     _ => (),
75                 }
76                 let (l_ty, r_ty) = (cx.tables.expr_ty(l), cx.tables.expr_ty(r));
77                 if l_ty.is_integral() && r_ty.is_integral() {
78                     span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
79                     self.span = Some(expr.span);
80                 } else if l_ty.is_floating_point() && r_ty.is_floating_point() {
81                     span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
82                     self.span = Some(expr.span);
83                 }
84             },
85             hir::ExprKind::Unary(hir::UnOp::UnNeg, ref arg) => {
86                 let ty = cx.tables.expr_ty(arg);
87                 if ty.is_integral() {
88                     span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
89                     self.span = Some(expr.span);
90                 } else if ty.is_floating_point() {
91                     span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
92                     self.span = Some(expr.span);
93                 }
94             },
95             _ => (),
96         }
97     }
98
99     fn check_expr_post(&mut self, _: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
100         if Some(expr.span) == self.span {
101             self.span = None;
102         }
103     }
104 }