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.
22 pub INTEGER_ARITHMETIC,
24 "any integer arithmetic statement"
27 declare_clippy_lint! {
28 /// **What it does:** Checks for float arithmetic.
30 /// **Why is this bad?** For some embedded systems or kernel development, it
31 /// can be useful to rule out floating-point numbers.
33 /// **Known problems:** None.
42 "any floating-point arithmetic statement"
45 #[derive(Copy, Clone, Default)]
46 pub struct Arithmetic {
47 expr_span: Option<Span>,
48 /// This field is used to check whether expressions are constants, such as in enum discriminants
50 const_span: Option<Span>,
53 impl_lint_pass!(Arithmetic => [INTEGER_ARITHMETIC, FLOAT_ARITHMETIC]);
55 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Arithmetic {
56 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
57 if self.expr_span.is_some() {
61 if let Some(span) = self.const_span {
62 if span.contains(expr.span) {
67 hir::ExprKind::Binary(op, l, r) | hir::ExprKind::AssignOp(op, l, r) => {
71 | hir::BinOpKind::BitAnd
72 | hir::BinOpKind::BitOr
73 | hir::BinOpKind::BitXor
81 | hir::BinOpKind::Gt => return,
84 let (l_ty, r_ty) = (cx.tables.expr_ty(l), cx.tables.expr_ty(r));
85 if l_ty.is_integral() && r_ty.is_integral() {
86 span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
87 self.expr_span = Some(expr.span);
88 } else if l_ty.is_floating_point() && r_ty.is_floating_point() {
89 span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
90 self.expr_span = Some(expr.span);
93 hir::ExprKind::Unary(hir::UnOp::UnNeg, arg) => {
94 let ty = cx.tables.expr_ty(arg);
95 if constant_simple(cx, cx.tables, expr).is_none() {
97 span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
98 self.expr_span = Some(expr.span);
99 } else if ty.is_floating_point() {
100 span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
101 self.expr_span = Some(expr.span);
109 fn check_expr_post(&mut self, _: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
110 if Some(expr.span) == self.expr_span {
111 self.expr_span = None;
115 fn check_body(&mut self, cx: &LateContext<'_, '_>, body: &hir::Body) {
116 let body_owner = cx.tcx.hir().body_owner(body.id());
118 match cx.tcx.hir().body_owner_kind(body_owner) {
119 hir::BodyOwnerKind::Static(_) | hir::BodyOwnerKind::Const => {
120 let body_span = cx.tcx.hir().span(body_owner);
122 if let Some(span) = self.const_span {
123 if span.contains(body_span) {
127 self.const_span = Some(body_span);
129 hir::BodyOwnerKind::Fn | hir::BodyOwnerKind::Closure => (),
133 fn check_body_post(&mut self, cx: &LateContext<'_, '_>, body: &hir::Body) {
134 let body_owner = cx.tcx.hir().body_owner(body.id());
135 let body_span = cx.tcx.hir().span(body_owner);
137 if let Some(span) = self.const_span {
138 if span.contains(body_span) {
142 self.const_span = None;