|
1 |
| -use rustc_hir::{Expr, ExprKind}; |
| 1 | +use rustc_hir::{Body, Expr, ExprKind, UnOp}; |
2 | 2 | use rustc_lint::{LateContext, LateLintPass};
|
3 | 3 | use rustc_session::{declare_tool_lint, impl_lint_pass};
|
4 | 4 |
|
5 | 5 | mod absurd_extreme_comparisons;
|
| 6 | +mod numeric_arithmetic; |
6 | 7 |
|
7 | 8 | declare_clippy_lint! {
|
8 | 9 | /// ### What it does
|
@@ -36,16 +37,90 @@ declare_clippy_lint! {
|
36 | 37 | "a comparison with a maximum or minimum value that is always true or false"
|
37 | 38 | }
|
38 | 39 |
|
39 |
| -pub struct Operators; |
| 40 | +declare_clippy_lint! { |
| 41 | + /// ### What it does |
| 42 | + /// Checks for integer arithmetic operations which could overflow or panic. |
| 43 | + /// |
| 44 | + /// Specifically, checks for any operators (`+`, `-`, `*`, `<<`, etc) which are capable |
| 45 | + /// of overflowing according to the [Rust |
| 46 | + /// Reference](https://doc.rust-lang.org/reference/expressions/operator-expr.html#overflow), |
| 47 | + /// or which can panic (`/`, `%`). No bounds analysis or sophisticated reasoning is |
| 48 | + /// attempted. |
| 49 | + /// |
| 50 | + /// ### Why is this bad? |
| 51 | + /// Integer overflow will trigger a panic in debug builds or will wrap in |
| 52 | + /// release mode. Division by zero will cause a panic in either mode. In some applications one |
| 53 | + /// wants explicitly checked, wrapping or saturating arithmetic. |
| 54 | + /// |
| 55 | + /// ### Example |
| 56 | + /// ```rust |
| 57 | + /// # let a = 0; |
| 58 | + /// a + 1; |
| 59 | + /// ``` |
| 60 | + #[clippy::version = "pre 1.29.0"] |
| 61 | + pub INTEGER_ARITHMETIC, |
| 62 | + restriction, |
| 63 | + "any integer arithmetic expression which could overflow or panic" |
| 64 | +} |
| 65 | + |
| 66 | +declare_clippy_lint! { |
| 67 | + /// ### What it does |
| 68 | + /// Checks for float arithmetic. |
| 69 | + /// |
| 70 | + /// ### Why is this bad? |
| 71 | + /// For some embedded systems or kernel development, it |
| 72 | + /// can be useful to rule out floating-point numbers. |
| 73 | + /// |
| 74 | + /// ### Example |
| 75 | + /// ```rust |
| 76 | + /// # let a = 0.0; |
| 77 | + /// a + 1.0; |
| 78 | + /// ``` |
| 79 | + #[clippy::version = "pre 1.29.0"] |
| 80 | + pub FLOAT_ARITHMETIC, |
| 81 | + restriction, |
| 82 | + "any floating-point arithmetic statement" |
| 83 | +} |
| 84 | + |
| 85 | +#[derive(Default)] |
| 86 | +pub struct Operators { |
| 87 | + arithmetic_context: numeric_arithmetic::Context, |
| 88 | +} |
40 | 89 | impl_lint_pass!(Operators => [
|
41 | 90 | ABSURD_EXTREME_COMPARISONS,
|
| 91 | + INTEGER_ARITHMETIC, |
| 92 | + FLOAT_ARITHMETIC, |
42 | 93 | ]);
|
43 | 94 | impl<'tcx> LateLintPass<'tcx> for Operators {
|
44 | 95 | fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
|
45 |
| - if let ExprKind::Binary(op, lhs, rhs) = e.kind { |
46 |
| - if !e.span.from_expansion() { |
47 |
| - absurd_extreme_comparisons::check(cx, e, op.node, lhs, rhs); |
48 |
| - } |
| 96 | + match e.kind { |
| 97 | + ExprKind::Binary(op, lhs, rhs) => { |
| 98 | + if !e.span.from_expansion() { |
| 99 | + absurd_extreme_comparisons::check(cx, e, op.node, lhs, rhs); |
| 100 | + } |
| 101 | + self.arithmetic_context.check_binary(cx, e, op.node, lhs, rhs); |
| 102 | + }, |
| 103 | + ExprKind::AssignOp(op, lhs, rhs) => { |
| 104 | + self.arithmetic_context.check_binary(cx, e, op.node, lhs, rhs); |
| 105 | + }, |
| 106 | + ExprKind::Unary(op, arg) => { |
| 107 | + if op == UnOp::Neg { |
| 108 | + self.arithmetic_context.check_negate(cx, e, arg); |
| 109 | + } |
| 110 | + }, |
| 111 | + _ => (), |
49 | 112 | }
|
50 | 113 | }
|
| 114 | + |
| 115 | + fn check_expr_post(&mut self, _: &LateContext<'_>, e: &Expr<'_>) { |
| 116 | + self.arithmetic_context.expr_post(e.hir_id); |
| 117 | + } |
| 118 | + |
| 119 | + fn check_body(&mut self, cx: &LateContext<'tcx>, b: &'tcx Body<'_>) { |
| 120 | + self.arithmetic_context.enter_body(cx, b); |
| 121 | + } |
| 122 | + |
| 123 | + fn check_body_post(&mut self, cx: &LateContext<'tcx>, b: &'tcx Body<'_>) { |
| 124 | + self.arithmetic_context.body_post(cx, b); |
| 125 | + } |
51 | 126 | }
|
0 commit comments