1 use crate::consts::constant_simple;
2 use crate::utils::span_lint;
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use rustc::{declare_tool_lint, impl_lint_pass};
6 use syntax::source_map::Span;
9 /// **What it does:** Checks for plain integer arithmetic.
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
15 /// **Known problems:** None.
21 pub INTEGER_ARITHMETIC,
23 "any integer arithmetic statement"
26 declare_clippy_lint! {
27 /// **What it does:** Checks for float arithmetic.
29 /// **Why is this bad?** For some embedded systems or kernel development, it
30 /// can be useful to rule out floating-point numbers.
32 /// **Known problems:** None.
40 "any floating-point arithmetic statement"
43 #[derive(Copy, Clone, Default)]
44 pub struct Arithmetic {
45 expr_span: Option<Span>,
46 /// This field is used to check whether expressions are constants, such as in enum discriminants
48 const_span: Option<Span>,
51 impl_lint_pass!(Arithmetic => [INTEGER_ARITHMETIC, FLOAT_ARITHMETIC]);
53 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Arithmetic {
54 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
55 if self.expr_span.is_some() {
59 if let Some(span) = self.const_span {
60 if span.contains(expr.span) {
65 hir::ExprKind::Binary(op, l, r) => {
69 | hir::BinOpKind::BitAnd
70 | hir::BinOpKind::BitOr
71 | hir::BinOpKind::BitXor
79 | hir::BinOpKind::Gt => return,
82 let (l_ty, r_ty) = (cx.tables.expr_ty(l), cx.tables.expr_ty(r));
83 if l_ty.is_integral() && r_ty.is_integral() {
84 span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
85 self.expr_span = Some(expr.span);
86 } else if l_ty.is_floating_point() && r_ty.is_floating_point() {
87 span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
88 self.expr_span = Some(expr.span);
91 hir::ExprKind::Unary(hir::UnOp::UnNeg, arg) => {
92 let ty = cx.tables.expr_ty(arg);
94 if constant_simple(cx, cx.tables, expr).is_none() {
95 span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
96 self.expr_span = Some(expr.span);
98 } else if ty.is_floating_point() {
99 span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
100 self.expr_span = Some(expr.span);
107 fn check_expr_post(&mut self, _: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
108 if Some(expr.span) == self.expr_span {
109 self.expr_span = None;
113 fn check_body(&mut self, cx: &LateContext<'_, '_>, body: &hir::Body) {
114 let body_owner = cx.tcx.hir().body_owner(body.id());
116 match cx.tcx.hir().body_owner_kind(body_owner) {
117 hir::BodyOwnerKind::Static(_) | hir::BodyOwnerKind::Const => {
118 let body_span = cx.tcx.hir().span(body_owner);
120 if let Some(span) = self.const_span {
121 if span.contains(body_span) {
125 self.const_span = Some(body_span);
127 hir::BodyOwnerKind::Fn | hir::BodyOwnerKind::Closure => (),
131 fn check_body_post(&mut self, cx: &LateContext<'_, '_>, body: &hir::Body) {
132 let body_owner = cx.tcx.hir().body_owner(body.id());
133 let body_span = cx.tcx.hir().span(body_owner);
135 if let Some(span) = self.const_span {
136 if span.contains(body_span) {
140 self.const_span = None;