1 use clippy_utils::consts::constant_simple;
2 use clippy_utils::diagnostics::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<'tcx> LateLintPass<'tcx> for Arithmetic {
62 fn check_expr(&mut self, cx: &LateContext<'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.typeck_results().expr_ty(l), cx.typeck_results().expr_ty(r));
90 if l_ty.peel_refs().is_integral() && r_ty.peel_refs().is_integral() {
92 hir::BinOpKind::Div | hir::BinOpKind::Rem => match &r.kind {
93 hir::ExprKind::Lit(_lit) => (),
94 hir::ExprKind::Unary(hir::UnOp::Neg, expr) => {
95 if let hir::ExprKind::Lit(lit) = &expr.kind {
96 if let rustc_ast::ast::LitKind::Int(1, _) = lit.node {
97 span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
98 self.expr_span = Some(expr.span);
103 span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
104 self.expr_span = Some(expr.span);
108 span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
109 self.expr_span = Some(expr.span);
112 } else if r_ty.peel_refs().is_floating_point() && r_ty.peel_refs().is_floating_point() {
113 span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
114 self.expr_span = Some(expr.span);
117 hir::ExprKind::Unary(hir::UnOp::Neg, arg) => {
118 let ty = cx.typeck_results().expr_ty(arg);
119 if constant_simple(cx, cx.typeck_results(), expr).is_none() {
120 if ty.is_integral() {
121 span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
122 self.expr_span = Some(expr.span);
123 } else if ty.is_floating_point() {
124 span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
125 self.expr_span = Some(expr.span);
133 fn check_expr_post(&mut self, _: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
134 if Some(expr.span) == self.expr_span {
135 self.expr_span = None;
139 fn check_body(&mut self, cx: &LateContext<'_>, body: &hir::Body<'_>) {
140 let body_owner = cx.tcx.hir().body_owner(body.id());
142 match cx.tcx.hir().body_owner_kind(body_owner) {
143 hir::BodyOwnerKind::Static(_) | hir::BodyOwnerKind::Const => {
144 let body_span = cx.tcx.hir().span(body_owner);
146 if let Some(span) = self.const_span {
147 if span.contains(body_span) {
151 self.const_span = Some(body_span);
153 hir::BodyOwnerKind::Fn | hir::BodyOwnerKind::Closure => (),
157 fn check_body_post(&mut self, cx: &LateContext<'_>, body: &hir::Body<'_>) {
158 let body_owner = cx.tcx.hir().body_owner(body.id());
159 let body_span = cx.tcx.hir().span(body_owner);
161 if let Some(span) = self.const_span {
162 if span.contains(body_span) {
166 self.const_span = None;