]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/arithmetic.rs
Rollup merge of #89090 - cjgillot:bare-dyn, r=jackh726
[rust.git] / src / tools / clippy / clippy_lints / src / arithmetic.rs
1 use clippy_utils::consts::constant_simple;
2 use clippy_utils::diagnostics::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
10     /// Checks for integer arithmetic operations which could overflow or panic.
11     ///
12     /// Specifically, checks for any operators (`+`, `-`, `*`, `<<`, etc) which are capable
13     /// of overflowing according to the [Rust
14     /// Reference](https://doc.rust-lang.org/reference/expressions/operator-expr.html#overflow),
15     /// or which can panic (`/`, `%`). No bounds analysis or sophisticated reasoning is
16     /// attempted.
17     ///
18     /// ### Why is this bad?
19     /// Integer overflow will trigger a panic in debug builds or will wrap in
20     /// release mode. Division by zero will cause a panic in either mode. In some applications one
21     /// wants explicitly checked, wrapping or saturating arithmetic.
22     ///
23     /// ### Example
24     /// ```rust
25     /// # let a = 0;
26     /// a + 1;
27     /// ```
28     #[clippy::version = "pre 1.29.0"]
29     pub INTEGER_ARITHMETIC,
30     restriction,
31     "any integer arithmetic expression which could overflow or panic"
32 }
33
34 declare_clippy_lint! {
35     /// ### What it does
36     /// Checks for float arithmetic.
37     ///
38     /// ### Why is this bad?
39     /// For some embedded systems or kernel development, it
40     /// can be useful to rule out floating-point numbers.
41     ///
42     /// ### Example
43     /// ```rust
44     /// # let a = 0.0;
45     /// a + 1.0;
46     /// ```
47     #[clippy::version = "pre 1.29.0"]
48     pub FLOAT_ARITHMETIC,
49     restriction,
50     "any floating-point arithmetic statement"
51 }
52
53 #[derive(Copy, Clone, Default)]
54 pub struct Arithmetic {
55     expr_span: Option<Span>,
56     /// This field is used to check whether expressions are constants, such as in enum discriminants
57     /// and consts
58     const_span: Option<Span>,
59 }
60
61 impl_lint_pass!(Arithmetic => [INTEGER_ARITHMETIC, FLOAT_ARITHMETIC]);
62
63 impl<'tcx> LateLintPass<'tcx> for Arithmetic {
64     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
65         if self.expr_span.is_some() {
66             return;
67         }
68
69         if let Some(span) = self.const_span {
70             if span.contains(expr.span) {
71                 return;
72             }
73         }
74         match &expr.kind {
75             hir::ExprKind::Binary(op, l, r) | hir::ExprKind::AssignOp(op, l, r) => {
76                 match op.node {
77                     hir::BinOpKind::And
78                     | hir::BinOpKind::Or
79                     | hir::BinOpKind::BitAnd
80                     | hir::BinOpKind::BitOr
81                     | hir::BinOpKind::BitXor
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
91                 let (l_ty, r_ty) = (cx.typeck_results().expr_ty(l), cx.typeck_results().expr_ty(r));
92                 if l_ty.peel_refs().is_integral() && r_ty.peel_refs().is_integral() {
93                     match op.node {
94                         hir::BinOpKind::Div | hir::BinOpKind::Rem => match &r.kind {
95                             hir::ExprKind::Lit(_lit) => (),
96                             hir::ExprKind::Unary(hir::UnOp::Neg, expr) => {
97                                 if let hir::ExprKind::Lit(lit) = &expr.kind {
98                                     if let rustc_ast::ast::LitKind::Int(1, _) = lit.node {
99                                         span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
100                                         self.expr_span = Some(expr.span);
101                                     }
102                                 }
103                             },
104                             _ => {
105                                 span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
106                                 self.expr_span = Some(expr.span);
107                             },
108                         },
109                         _ => {
110                             span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
111                             self.expr_span = Some(expr.span);
112                         },
113                     }
114                 } else if r_ty.peel_refs().is_floating_point() && r_ty.peel_refs().is_floating_point() {
115                     span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
116                     self.expr_span = Some(expr.span);
117                 }
118             },
119             hir::ExprKind::Unary(hir::UnOp::Neg, arg) => {
120                 let ty = cx.typeck_results().expr_ty(arg);
121                 if constant_simple(cx, cx.typeck_results(), expr).is_none() {
122                     if ty.is_integral() {
123                         span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
124                         self.expr_span = Some(expr.span);
125                     } else if ty.is_floating_point() {
126                         span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
127                         self.expr_span = Some(expr.span);
128                     }
129                 }
130             },
131             _ => (),
132         }
133     }
134
135     fn check_expr_post(&mut self, _: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
136         if Some(expr.span) == self.expr_span {
137             self.expr_span = None;
138         }
139     }
140
141     fn check_body(&mut self, cx: &LateContext<'_>, body: &hir::Body<'_>) {
142         let body_owner = cx.tcx.hir().body_owner(body.id());
143
144         match cx.tcx.hir().body_owner_kind(body_owner) {
145             hir::BodyOwnerKind::Static(_) | hir::BodyOwnerKind::Const => {
146                 let body_span = cx.tcx.hir().span(body_owner);
147
148                 if let Some(span) = self.const_span {
149                     if span.contains(body_span) {
150                         return;
151                     }
152                 }
153                 self.const_span = Some(body_span);
154             },
155             hir::BodyOwnerKind::Fn | hir::BodyOwnerKind::Closure => (),
156         }
157     }
158
159     fn check_body_post(&mut self, cx: &LateContext<'_>, body: &hir::Body<'_>) {
160         let body_owner = cx.tcx.hir().body_owner(body.id());
161         let body_span = cx.tcx.hir().span(body_owner);
162
163         if let Some(span) = self.const_span {
164             if span.contains(body_span) {
165                 return;
166             }
167         }
168         self.const_span = None;
169     }
170 }