[arithmetic_side_effects] Fix #10583

This commit is contained in:
Caio 2023-04-02 07:50:25 -03:00
parent ac4838c554
commit 05650b7215
3 changed files with 27 additions and 3 deletions

View File

@ -154,10 +154,18 @@ impl ArithmeticSideEffects {
Self::literal_integer(cx, actual_rhs),
) {
(None, None) => false,
(None, Some(n)) | (Some(n), None) => match (&op.node, n) {
(None, Some(n)) => match (&op.node, n) {
// Division and module are always valid if applied to non-zero integers
(hir::BinOpKind::Div | hir::BinOpKind::Rem, local_n) if local_n != 0 => true,
// Addition or subtracting zeros is always a no-op
// Adding or subtracting zeros is always a no-op
(hir::BinOpKind::Add | hir::BinOpKind::Sub, 0)
// Multiplication by 1 or 0 will never overflow
| (hir::BinOpKind::Mul, 0 | 1)
=> true,
_ => false,
},
(Some(n), None) => match (&op.node, n) {
// Adding or subtracting zeros is always a no-op
(hir::BinOpKind::Add | hir::BinOpKind::Sub, 0)
// Multiplication by 1 or 0 will never overflow
| (hir::BinOpKind::Mul, 0 | 1)

View File

@ -425,4 +425,8 @@ pub fn integer_arithmetic() {
i ^= i;
}
pub fn issue_10583(a: u16) -> u16 {
10 / a
}
fn main() {}

View File

@ -576,6 +576,12 @@ error: arithmetic operation that can potentially result in unexpected side-effec
LL | i * 2;
| ^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:394:5
|
LL | 1 % i / 2;
| ^^^^^
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:395:5
|
@ -642,5 +648,11 @@ error: arithmetic operation that can potentially result in unexpected side-effec
LL | i %= var2;
| ^^^^^^^^^
error: aborting due to 107 previous errors
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:429:5
|
LL | 10 / a
| ^^^^^^
error: aborting due to 109 previous errors