Skip to content

Commit

Permalink
Auto merge of rust-lang#6229 - henil:improve-integer-division-lint, r…
Browse files Browse the repository at this point in the history
…=phansch

Update the existing arithmetic lint

re: rust-lang#6209

Updates the lint to not the error message if RHS of binary operation `/` of `%` is a literal/constant that is not `0` or `-1`, as suggested [here](rust-lang/rust-clippy#6209 (comment))

changelog: Expand [`integer_arithmetic`] to work with RHS literals and constants
  • Loading branch information
bors committed Oct 30, 2020
2 parents 74d8fbb + fa0a78b commit 0be6544
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 30 deletions.
25 changes: 22 additions & 3 deletions clippy_lints/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,28 @@ 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) => (),
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
92 changes: 65 additions & 27 deletions tests/ui/integer_arithmetic.stderr
Original file line number Diff line number Diff line change
@@ -1,131 +1,169 @@
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:38:11
|
LL | i /= 2;
| ^^^^^^
LL | i /= -1;
| ^

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

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

error: integer arithmetic detected
--> $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:36:5
--> $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 27 previous errors

0 comments on commit 0be6544

Please sign in to comment.