Skip to content

Commit

Permalink
Update existing arithmetic lint and add new tests related to it.
Browse files Browse the repository at this point in the history
  • Loading branch information
henil committed Oct 29, 2020
1 parent 6d5cd6e commit e97602e
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 28 deletions.
30 changes: 27 additions & 3 deletions clippy_lints/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,33 @@ impl<'tcx> LateLintPass<'tcx> for Arithmetic {

let (l_ty, r_ty) = (cx.typeck_results().expr_ty(l), cx.typeck_results().expr_ty(r));
if l_ty.peel_refs().is_integral() && r_ty.peel_refs().is_integral() {
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
self.expr_span = Some(expr.span);
} else if l_ty.peel_refs().is_floating_point() && r_ty.peel_refs().is_floating_point() {
match op.node {
hir::BinOpKind::Div | hir::BinOpKind::Rem => match &r.kind {
hir::ExprKind::Lit(lit) => {
if let rustc_ast::ast::LitKind::Int(0, _) = lit.node {
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
self.expr_span = Some(expr.span);
}
},
hir::ExprKind::Unary(hir::UnOp::UnNeg, expr) => {
if let hir::ExprKind::Lit(lit) = &expr.kind {
if let rustc_ast::ast::LitKind::Int(1, _) = lit.node {
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
self.expr_span = Some(expr.span);
}
}
},
_ => {
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
self.expr_span = Some(expr.span);
},
},
_ => {
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
self.expr_span = Some(expr.span);
},
}
} else if r_ty.peel_refs().is_floating_point() && r_ty.peel_refs().is_floating_point() {
span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
self.expr_span = Some(expr.span);
}
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/integer_arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#[rustfmt::skip]
fn main() {
let mut i = 1i32;
let mut var1 = 0i32;
let mut var2 = -1i32;
1 + i;
i * 2;
1 %
Expand All @@ -32,7 +34,15 @@ fn main() {
i -= 1;
i *= 2;
i /= 2;
i /= 0;
i /= -1;
i /= var1;
i /= var2;
i %= 2;
i %= 0;
i %= -1;
i %= var1;
i %= var2;
i <<= 3;
i >>= 2;

Expand Down
100 changes: 75 additions & 25 deletions tests/ui/integer_arithmetic.stderr
Original file line number Diff line number Diff line change
@@ -1,131 +1,181 @@
error: this operation will panic at runtime
--> $DIR/integer_arithmetic.rs:37:5
|
LL | i /= 0;
| ^^^^^^ attempt to divide `_` by zero
|
= note: `#[deny(unconditional_panic)]` on by default

error: this operation will panic at runtime
--> $DIR/integer_arithmetic.rs:42:5
|
LL | i %= 0;
| ^^^^^^ attempt to calculate the remainder of `_` with a divisor of zero

error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:14:5
--> $DIR/integer_arithmetic.rs:16:5
|
LL | 1 + i;
| ^^^^^
|
= note: `-D clippy::integer-arithmetic` implied by `-D warnings`

error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:15:5
--> $DIR/integer_arithmetic.rs:17:5
|
LL | i * 2;
| ^^^^^

error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:16:5
--> $DIR/integer_arithmetic.rs:18:5
|
LL | / 1 %
LL | | i / 2; // no error, this is part of the expression in the preceding line
| |_________^
| |_____^

error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:18:5
--> $DIR/integer_arithmetic.rs:20:5
|
LL | i - 2 + 2 - i;
| ^^^^^^^^^^^^^

error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:19:5
--> $DIR/integer_arithmetic.rs:21:5
|
LL | -i;
| ^^

error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:20:5
--> $DIR/integer_arithmetic.rs:22:5
|
LL | i >> 1;
| ^^^^^^

error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:21:5
--> $DIR/integer_arithmetic.rs:23:5
|
LL | i << 1;
| ^^^^^^

error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:31:5
--> $DIR/integer_arithmetic.rs:33:5
|
LL | i += 1;
| ^^^^^^

error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:32:5
--> $DIR/integer_arithmetic.rs:34:5
|
LL | i -= 1;
| ^^^^^^

error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:33:5
--> $DIR/integer_arithmetic.rs:35:5
|
LL | i *= 2;
| ^^^^^^

error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:34:5
--> $DIR/integer_arithmetic.rs:37:5
|
LL | i /= 2;
LL | i /= 0;
| ^^^^^^

error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:35:5
--> $DIR/integer_arithmetic.rs:38:11
|
LL | i /= -1;
| ^

error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:39:5
|
LL | i /= var1;
| ^^^^^^^^^

error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:40:5
|
LL | i /= var2;
| ^^^^^^^^^

error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:42:5
|
LL | i %= 2;
LL | i %= 0;
| ^^^^^^

error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:36:5
--> $DIR/integer_arithmetic.rs:43:11
|
LL | i %= -1;
| ^

error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:44:5
|
LL | i %= var1;
| ^^^^^^^^^

error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:45:5
|
LL | i %= var2;
| ^^^^^^^^^

error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:46:5
|
LL | i <<= 3;
| ^^^^^^^

error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:37:5
--> $DIR/integer_arithmetic.rs:47:5
|
LL | i >>= 2;
| ^^^^^^^

error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:79:5
--> $DIR/integer_arithmetic.rs:89:5
|
LL | 3 + &1;
| ^^^^^^

error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:80:5
--> $DIR/integer_arithmetic.rs:90:5
|
LL | &3 + 1;
| ^^^^^^

error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:81:5
--> $DIR/integer_arithmetic.rs:91:5
|
LL | &3 + &1;
| ^^^^^^^

error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:86:5
--> $DIR/integer_arithmetic.rs:96:5
|
LL | a + x
| ^^^^^

error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:90:5
--> $DIR/integer_arithmetic.rs:100:5
|
LL | x + y
| ^^^^^

error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:94:5
--> $DIR/integer_arithmetic.rs:104:5
|
LL | x + y
| ^^^^^

error: integer arithmetic detected
--> $DIR/integer_arithmetic.rs:98:5
--> $DIR/integer_arithmetic.rs:108:5
|
LL | (&x + &y)
| ^^^^^^^^^

error: aborting due to 21 previous errors
error: aborting due to 29 previous errors

0 comments on commit e97602e

Please sign in to comment.