3 use syntax::codemap::Span;
6 /// **What it does:** This lint checks for plain integer arithmetic
8 /// **Why is this bad?** This is only checked against overflow in debug builds.
9 /// In some applications one wants explicitly checked, wrapping or saturating
12 /// **Known problems:** None
19 pub INTEGER_ARITHMETIC,
21 "Any integer arithmetic statement"
24 /// **What it does:** This lint checks for float arithmetic
26 /// **Why is this bad?** For some embedded systems or kernel development, it
27 /// can be useful to rule out floating-point numbers
29 /// **Known problems:** None
38 "Any floating-point arithmetic statement"
41 #[derive(Copy, Clone, Default)]
42 pub struct Arithmetic {
46 impl LintPass for Arithmetic {
47 fn get_lints(&self) -> LintArray {
48 lint_array!(INTEGER_ARITHMETIC, FLOAT_ARITHMETIC)
52 impl LateLintPass for Arithmetic {
53 fn check_expr(&mut self, cx: &LateContext, expr: &hir::Expr) {
54 if let Some(_) = self.span { return; }
56 hir::ExprBinary(ref op, ref l, ref r) => {
58 hir::BiAnd | hir::BiOr | hir::BiBitAnd |
59 hir::BiBitOr | hir::BiBitXor | hir::BiShl | hir::BiShr |
60 hir::BiEq | hir::BiLt | hir::BiLe | hir::BiNe | hir::BiGe |
64 let (l_ty, r_ty) = (cx.tcx.expr_ty(l), cx.tcx.expr_ty(r));
65 if l_ty.is_integral() && r_ty.is_integral() {
69 "integer arithmetic detected");
70 self.span = Some(expr.span);
71 } else if l_ty.is_floating_point() && r_ty.is_floating_point() {
75 "floating-point arithmetic detected");
76 self.span = Some(expr.span);
79 hir::ExprUnary(hir::UnOp::UnNeg, ref arg) => {
80 let ty = cx.tcx.expr_ty(arg);
85 "integer arithmetic detected");
86 self.span = Some(expr.span);
87 } else if ty.is_floating_point() {
91 "floating-point arithmetic detected");
92 self.span = Some(expr.span);
99 fn check_expr_post(&mut self, _: &LateContext, expr: &hir::Expr) {
100 if Some(expr.span) == self.span {