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, lint_array};
6 use syntax::source_map::Span;
8 /// **What it does:** Checks for plain integer arithmetic.
10 /// **Why is this bad?** This is only checked against overflow in debug builds.
11 /// In some applications one wants explicitly checked, wrapping or saturating
14 /// **Known problems:** None.
20 declare_clippy_lint! {
21 pub INTEGER_ARITHMETIC,
23 "any integer arithmetic statement"
26 /// **What it does:** Checks for float arithmetic.
28 /// **Why is this bad?** For some embedded systems or kernel development, it
29 /// can be useful to rule out floating-point numbers.
31 /// **Known problems:** None.
37 declare_clippy_lint! {
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 LintPass for Arithmetic {
52 fn get_lints(&self) -> LintArray {
53 lint_array!(INTEGER_ARITHMETIC, FLOAT_ARITHMETIC)
57 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Arithmetic {
58 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
59 if self.expr_span.is_some() {
63 if let Some(span) = self.const_span {
64 if span.contains(expr.span) {
69 hir::ExprKind::Binary(op, l, r) => {
73 | hir::BinOpKind::BitAnd
74 | hir::BinOpKind::BitOr
75 | hir::BinOpKind::BitXor
83 | hir::BinOpKind::Gt => return,
86 let (l_ty, r_ty) = (cx.tables.expr_ty(l), cx.tables.expr_ty(r));
87 if l_ty.is_integral() && r_ty.is_integral() {
88 span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
89 self.expr_span = Some(expr.span);
90 } else if l_ty.is_floating_point() && r_ty.is_floating_point() {
91 span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
92 self.expr_span = Some(expr.span);
95 hir::ExprKind::Unary(hir::UnOp::UnNeg, arg) => {
96 let ty = cx.tables.expr_ty(arg);
98 if constant_simple(cx, cx.tables, expr).is_none() {
99 span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
100 self.expr_span = Some(expr.span);
102 } else if ty.is_floating_point() {
103 span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
104 self.expr_span = Some(expr.span);
111 fn check_expr_post(&mut self, _: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
112 if Some(expr.span) == self.expr_span {
113 self.expr_span = None;
117 fn check_body(&mut self, cx: &LateContext<'_, '_>, body: &hir::Body) {
118 let body_owner = cx.tcx.hir().body_owner(body.id());
120 match cx.tcx.hir().body_owner_kind(body_owner) {
121 hir::BodyOwnerKind::Static(_) | hir::BodyOwnerKind::Const => {
122 let body_span = cx.tcx.hir().span(body_owner);
124 if let Some(span) = self.const_span {
125 if span.contains(body_span) {
129 self.const_span = Some(body_span);
131 hir::BodyOwnerKind::Fn => (),
135 fn check_body_post(&mut self, cx: &LateContext<'_, '_>, body: &hir::Body) {
136 let body_owner = cx.tcx.hir().body_owner(body.id());
137 let body_span = cx.tcx.hir().span(body_owner);
139 if let Some(span) = self.const_span {
140 if span.contains(body_span) {
144 self.const_span = None;