1 use crate::consts::constant_simple;
2 use crate::utils::span_lint;
4 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_session::{declare_tool_lint, impl_lint_pass};
6 use rustc_span::source_map::Span;
9 /// **What it does:** Checks for integer arithmetic operations which could overflow or panic.
11 /// Specifically, checks for any operators (`+`, `-`, `*`, `<<`, etc) which are capable
12 /// of overflowing according to the [Rust
13 /// Reference](https://doc.rust-lang.org/reference/expressions/operator-expr.html#overflow),
14 /// or which can panic (`/`, `%`). No bounds analysis or sophisticated reasoning is
17 /// **Why is this bad?** Integer overflow will trigger a panic in debug builds or will wrap in
18 /// release mode. Division by zero will cause a panic in either mode. In some applications one
19 /// wants explicitly checked, wrapping or saturating arithmetic.
21 /// **Known problems:** None.
28 pub INTEGER_ARITHMETIC,
30 "any integer arithmetic expression which could overflow or panic"
33 declare_clippy_lint! {
34 /// **What it does:** Checks for float arithmetic.
36 /// **Why is this bad?** For some embedded systems or kernel development, it
37 /// can be useful to rule out floating-point numbers.
39 /// **Known problems:** None.
48 "any floating-point arithmetic statement"
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
56 const_span: Option<Span>,
59 impl_lint_pass!(Arithmetic => [INTEGER_ARITHMETIC, FLOAT_ARITHMETIC]);
61 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Arithmetic {
62 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr<'_>) {
63 if self.expr_span.is_some() {
67 if let Some(span) = self.const_span {
68 if span.contains(expr.span) {
73 hir::ExprKind::Binary(op, l, r) | hir::ExprKind::AssignOp(op, l, r) => {
77 | hir::BinOpKind::BitAnd
78 | hir::BinOpKind::BitOr
79 | hir::BinOpKind::BitXor
85 | hir::BinOpKind::Gt => return,
89 let (l_ty, r_ty) = (cx.tables.expr_ty(l), cx.tables.expr_ty(r));
90 if l_ty.peel_refs().is_integral() && r_ty.peel_refs().is_integral() {
91 span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
92 self.expr_span = Some(expr.span);
93 } else if l_ty.peel_refs().is_floating_point() && r_ty.peel_refs().is_floating_point() {
94 span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
95 self.expr_span = Some(expr.span);
98 hir::ExprKind::Unary(hir::UnOp::UnNeg, arg) => {
99 let ty = cx.tables.expr_ty(arg);
100 if constant_simple(cx, cx.tables, expr).is_none() {
101 if ty.is_integral() {
102 span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
103 self.expr_span = Some(expr.span);
104 } else if ty.is_floating_point() {
105 span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
106 self.expr_span = Some(expr.span);
114 fn check_expr_post(&mut self, _: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr<'_>) {
115 if Some(expr.span) == self.expr_span {
116 self.expr_span = None;
120 fn check_body(&mut self, cx: &LateContext<'_, '_>, body: &hir::Body<'_>) {
121 let body_owner = cx.tcx.hir().body_owner(body.id());
123 match cx.tcx.hir().body_owner_kind(body_owner) {
124 hir::BodyOwnerKind::Static(_) | hir::BodyOwnerKind::Const => {
125 let body_span = cx.tcx.hir().span(body_owner);
127 if let Some(span) = self.const_span {
128 if span.contains(body_span) {
132 self.const_span = Some(body_span);
134 hir::BodyOwnerKind::Fn | hir::BodyOwnerKind::Closure => (),
138 fn check_body_post(&mut self, cx: &LateContext<'_, '_>, body: &hir::Body<'_>) {
139 let body_owner = cx.tcx.hir().body_owner(body.id());
140 let body_span = cx.tcx.hir().span(body_owner);
142 if let Some(span) = self.const_span {
143 if span.contains(body_span) {
147 self.const_span = None;