1 use crate::consts::constant_simple;
2 use crate::utils::span_lint;
4 use rustc::impl_lint_pass;
5 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
6 use rustc_session::declare_tool_lint;
7 use syntax::source_map::Span;
10 /// **What it does:** Checks for plain integer arithmetic.
12 /// **Why is this bad?** This is only checked against overflow in debug builds.
13 /// In some applications one wants explicitly checked, wrapping or saturating
16 /// **Known problems:** None.
23 pub INTEGER_ARITHMETIC,
25 "any integer arithmetic statement"
28 declare_clippy_lint! {
29 /// **What it does:** Checks for float arithmetic.
31 /// **Why is this bad?** For some embedded systems or kernel development, it
32 /// can be useful to rule out floating-point numbers.
34 /// **Known problems:** None.
43 "any floating-point arithmetic statement"
46 #[derive(Copy, Clone, Default)]
47 pub struct Arithmetic {
48 expr_span: Option<Span>,
49 /// This field is used to check whether expressions are constants, such as in enum discriminants
51 const_span: Option<Span>,
54 impl_lint_pass!(Arithmetic => [INTEGER_ARITHMETIC, FLOAT_ARITHMETIC]);
56 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Arithmetic {
57 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
58 if self.expr_span.is_some() {
62 if let Some(span) = self.const_span {
63 if span.contains(expr.span) {
68 hir::ExprKind::Binary(op, l, r) | hir::ExprKind::AssignOp(op, l, r) => {
72 | hir::BinOpKind::BitAnd
73 | hir::BinOpKind::BitOr
74 | hir::BinOpKind::BitXor
82 | hir::BinOpKind::Gt => return,
85 let (l_ty, r_ty) = (cx.tables.expr_ty(l), cx.tables.expr_ty(r));
86 if l_ty.is_integral() && r_ty.is_integral() {
87 span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
88 self.expr_span = Some(expr.span);
89 } else if l_ty.is_floating_point() && r_ty.is_floating_point() {
90 span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
91 self.expr_span = Some(expr.span);
94 hir::ExprKind::Unary(hir::UnOp::UnNeg, arg) => {
95 let ty = cx.tables.expr_ty(arg);
96 if constant_simple(cx, cx.tables, expr).is_none() {
98 span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
99 self.expr_span = Some(expr.span);
100 } else if ty.is_floating_point() {
101 span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
102 self.expr_span = Some(expr.span);
110 fn check_expr_post(&mut self, _: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
111 if Some(expr.span) == self.expr_span {
112 self.expr_span = None;
116 fn check_body(&mut self, cx: &LateContext<'_, '_>, body: &hir::Body) {
117 let body_owner = cx.tcx.hir().body_owner(body.id());
119 match cx.tcx.hir().body_owner_kind(body_owner) {
120 hir::BodyOwnerKind::Static(_) | hir::BodyOwnerKind::Const => {
121 let body_span = cx.tcx.hir().span(body_owner);
123 if let Some(span) = self.const_span {
124 if span.contains(body_span) {
128 self.const_span = Some(body_span);
130 hir::BodyOwnerKind::Fn | hir::BodyOwnerKind::Closure => (),
134 fn check_body_post(&mut self, cx: &LateContext<'_, '_>, body: &hir::Body) {
135 let body_owner = cx.tcx.hir().body_owner(body.id());
136 let body_span = cx.tcx.hir().span(body_owner);
138 if let Some(span) = self.const_span {
139 if span.contains(body_span) {
143 self.const_span = None;