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)
56 fn name(&self) -> &'static str {
61 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Arithmetic {
62 fn check_expr(&mut self, cx: &LateContext<'a, '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) => {
77 | hir::BinOpKind::BitAnd
78 | hir::BinOpKind::BitOr
79 | hir::BinOpKind::BitXor
87 | hir::BinOpKind::Gt => return,
90 let (l_ty, r_ty) = (cx.tables.expr_ty(l), cx.tables.expr_ty(r));
91 if l_ty.is_integral() && r_ty.is_integral() {
92 span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
93 self.expr_span = Some(expr.span);
94 } else if l_ty.is_floating_point() && r_ty.is_floating_point() {
95 span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
96 self.expr_span = Some(expr.span);
99 hir::ExprKind::Unary(hir::UnOp::UnNeg, arg) => {
100 let ty = cx.tables.expr_ty(arg);
101 if ty.is_integral() {
102 if constant_simple(cx, cx.tables, expr).is_none() {
103 span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
104 self.expr_span = Some(expr.span);
106 } else if ty.is_floating_point() {
107 span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
108 self.expr_span = Some(expr.span);
115 fn check_expr_post(&mut self, _: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
116 if Some(expr.span) == self.expr_span {
117 self.expr_span = None;
121 fn check_body(&mut self, cx: &LateContext<'_, '_>, body: &hir::Body) {
122 let body_owner = cx.tcx.hir().body_owner(body.id());
124 match cx.tcx.hir().body_owner_kind(body_owner) {
125 hir::BodyOwnerKind::Static(_) | hir::BodyOwnerKind::Const => {
126 let body_span = cx.tcx.hir().span(body_owner);
128 if let Some(span) = self.const_span {
129 if span.contains(body_span) {
133 self.const_span = Some(body_span);
135 hir::BodyOwnerKind::Fn | hir::BodyOwnerKind::Closure => (),
139 fn check_body_post(&mut self, cx: &LateContext<'_, '_>, body: &hir::Body) {
140 let body_owner = cx.tcx.hir().body_owner(body.id());
141 let body_span = cx.tcx.hir().span(body_owner);
143 if let Some(span) = self.const_span {
144 if span.contains(body_span) {
148 self.const_span = None;