]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/arithmetic.rs
Auto merge of #5334 - flip1995:backport_remerge, 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 as hir;
4 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_session::{declare_tool_lint, impl_lint_pass};
6 use rustc_span::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     /// # let a = 0;
20     /// a + 1;
21     /// ```
22     pub INTEGER_ARITHMETIC,
23     restriction,
24     "any integer arithmetic statement"
25 }
26
27 declare_clippy_lint! {
28     /// **What it does:** Checks for float arithmetic.
29     ///
30     /// **Why is this bad?** For some embedded systems or kernel development, it
31     /// can be useful to rule out floating-point numbers.
32     ///
33     /// **Known problems:** None.
34     ///
35     /// **Example:**
36     /// ```rust
37     /// # let a = 0.0;
38     /// a + 1.0;
39     /// ```
40     pub FLOAT_ARITHMETIC,
41     restriction,
42     "any floating-point arithmetic statement"
43 }
44
45 #[derive(Copy, Clone, Default)]
46 pub struct Arithmetic {
47     expr_span: Option<Span>,
48     /// This field is used to check whether expressions are constants, such as in enum discriminants
49     /// and consts
50     const_span: Option<Span>,
51 }
52
53 impl_lint_pass!(Arithmetic => [INTEGER_ARITHMETIC, FLOAT_ARITHMETIC]);
54
55 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Arithmetic {
56     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr<'_>) {
57         if self.expr_span.is_some() {
58             return;
59         }
60
61         if let Some(span) = self.const_span {
62             if span.contains(expr.span) {
63                 return;
64             }
65         }
66         match &expr.kind {
67             hir::ExprKind::Binary(op, l, r) | hir::ExprKind::AssignOp(op, l, r) => {
68                 match op.node {
69                     hir::BinOpKind::And
70                     | hir::BinOpKind::Or
71                     | hir::BinOpKind::BitAnd
72                     | hir::BinOpKind::BitOr
73                     | hir::BinOpKind::BitXor
74                     | hir::BinOpKind::Shl
75                     | hir::BinOpKind::Shr
76                     | hir::BinOpKind::Eq
77                     | hir::BinOpKind::Lt
78                     | hir::BinOpKind::Le
79                     | hir::BinOpKind::Ne
80                     | hir::BinOpKind::Ge
81                     | hir::BinOpKind::Gt => return,
82                     _ => (),
83                 }
84
85                 let (l_ty, r_ty) = (cx.tables.expr_ty(l), cx.tables.expr_ty(r));
86                 if l_ty.peel_refs().is_integral() && r_ty.peel_refs().is_integral() {
87                     span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
88                     self.expr_span = Some(expr.span);
89                 } else if l_ty.peel_refs().is_floating_point() && r_ty.peel_refs().is_floating_point() {
90                     span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
91                     self.expr_span = Some(expr.span);
92                 }
93             },
94             hir::ExprKind::Unary(hir::UnOp::UnNeg, arg) => {
95                 let ty = cx.tables.expr_ty(arg);
96                 if constant_simple(cx, cx.tables, expr).is_none() {
97                     if ty.is_integral() {
98                         span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
99                         self.expr_span = Some(expr.span);
100                     } else if ty.is_floating_point() {
101                         span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
102                         self.expr_span = Some(expr.span);
103                     }
104                 }
105             },
106             _ => (),
107         }
108     }
109
110     fn check_expr_post(&mut self, _: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr<'_>) {
111         if Some(expr.span) == self.expr_span {
112             self.expr_span = None;
113         }
114     }
115
116     fn check_body(&mut self, cx: &LateContext<'_, '_>, body: &hir::Body<'_>) {
117         let body_owner = cx.tcx.hir().body_owner(body.id());
118
119         match cx.tcx.hir().body_owner_kind(body_owner) {
120             hir::BodyOwnerKind::Static(_) | hir::BodyOwnerKind::Const => {
121                 let body_span = cx.tcx.hir().span(body_owner);
122
123                 if let Some(span) = self.const_span {
124                     if span.contains(body_span) {
125                         return;
126                     }
127                 }
128                 self.const_span = Some(body_span);
129             },
130             hir::BodyOwnerKind::Fn | hir::BodyOwnerKind::Closure => (),
131         }
132     }
133
134     fn check_body_post(&mut self, cx: &LateContext<'_, '_>, body: &hir::Body<'_>) {
135         let body_owner = cx.tcx.hir().body_owner(body.id());
136         let body_span = cx.tcx.hir().span(body_owner);
137
138         if let Some(span) = self.const_span {
139             if span.contains(body_span) {
140                 return;
141             }
142         }
143         self.const_span = None;
144     }
145 }