1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
11 use crate::utils::span_lint;
12 use crate::rustc::hir;
13 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
14 use crate::rustc::{declare_tool_lint, lint_array};
15 use crate::syntax::source_map::Span;
17 /// **What it does:** Checks for plain integer arithmetic.
19 /// **Why is this bad?** This is only checked against overflow in debug builds.
20 /// In some applications one wants explicitly checked, wrapping or saturating
23 /// **Known problems:** None.
29 declare_clippy_lint! {
30 pub INTEGER_ARITHMETIC,
32 "any integer arithmetic statement"
35 /// **What it does:** Checks for float arithmetic.
37 /// **Why is this bad?** For some embedded systems or kernel development, it
38 /// can be useful to rule out floating-point numbers.
40 /// **Known problems:** None.
46 declare_clippy_lint! {
49 "any floating-point arithmetic statement"
52 #[derive(Copy, Clone, Default)]
53 pub struct Arithmetic {
54 expr_span: Option<Span>,
55 /// This field is used to check whether expressions are constants, such as in enum discriminants and consts
56 const_span: Option<Span>,
59 impl LintPass for Arithmetic {
60 fn get_lints(&self) -> LintArray {
61 lint_array!(INTEGER_ARITHMETIC, FLOAT_ARITHMETIC)
65 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Arithmetic {
66 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
67 if self.expr_span.is_some() {
71 if let Some(span) = self.const_span {
72 if span.contains(expr.span) {
77 hir::ExprKind::Binary(ref op, ref l, ref r) => {
81 | hir::BinOpKind::BitAnd
82 | hir::BinOpKind::BitOr
83 | hir::BinOpKind::BitXor
91 | hir::BinOpKind::Gt => return,
94 let (l_ty, r_ty) = (cx.tables.expr_ty(l), cx.tables.expr_ty(r));
95 if l_ty.is_integral() && r_ty.is_integral() {
96 span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
97 self.expr_span = Some(expr.span);
98 } else if l_ty.is_floating_point() && r_ty.is_floating_point() {
99 span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
100 self.expr_span = Some(expr.span);
103 hir::ExprKind::Unary(hir::UnOp::UnNeg, ref arg) => {
104 let ty = cx.tables.expr_ty(arg);
105 if ty.is_integral() {
106 span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
107 self.expr_span = Some(expr.span);
108 } else if ty.is_floating_point() {
109 span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
110 self.expr_span = Some(expr.span);
117 fn check_expr_post(&mut self, _: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
118 if Some(expr.span) == self.expr_span {
119 self.expr_span = None;
123 fn check_body(&mut self, cx: &LateContext<'_, '_>, body: &hir::Body) {
124 let body_owner = cx.tcx.hir.body_owner(body.id());
126 match cx.tcx.hir.body_owner_kind(body_owner) {
127 hir::BodyOwnerKind::Static(_)
128 | hir::BodyOwnerKind::Const => {
129 let body_span = cx.tcx.hir.span(body_owner);
131 if let Some(span) = self.const_span {
132 if span.contains(body_span) {
136 self.const_span = Some(body_span);
138 hir::BodyOwnerKind::Fn => (),
142 fn check_body_post(&mut self, cx: &LateContext<'_, '_>, body: &hir::Body) {
143 let body_owner = cx.tcx.hir.body_owner(body.id());
144 let body_span = cx.tcx.hir.span(body_owner);
146 if let Some(span) = self.const_span {
147 if span.contains(body_span) {
151 self.const_span = None;