Skip to content

Commit 05650b7

Browse files
committedApr 2, 2023
[arithmetic_side_effects] Fix rust-lang#10583
1 parent ac4838c commit 05650b7

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed
 

‎clippy_lints/src/operators/arithmetic_side_effects.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,18 @@ impl ArithmeticSideEffects {
154154
Self::literal_integer(cx, actual_rhs),
155155
) {
156156
(None, None) => false,
157-
(None, Some(n)) | (Some(n), None) => match (&op.node, n) {
157+
(None, Some(n)) => match (&op.node, n) {
158158
// Division and module are always valid if applied to non-zero integers
159159
(hir::BinOpKind::Div | hir::BinOpKind::Rem, local_n) if local_n != 0 => true,
160-
// Addition or subtracting zeros is always a no-op
160+
// Adding or subtracting zeros is always a no-op
161+
(hir::BinOpKind::Add | hir::BinOpKind::Sub, 0)
162+
// Multiplication by 1 or 0 will never overflow
163+
| (hir::BinOpKind::Mul, 0 | 1)
164+
=> true,
165+
_ => false,
166+
},
167+
(Some(n), None) => match (&op.node, n) {
168+
// Adding or subtracting zeros is always a no-op
161169
(hir::BinOpKind::Add | hir::BinOpKind::Sub, 0)
162170
// Multiplication by 1 or 0 will never overflow
163171
| (hir::BinOpKind::Mul, 0 | 1)

‎tests/ui/arithmetic_side_effects.rs

+4
Original file line numberDiff line numberDiff line change
@@ -425,4 +425,8 @@ pub fn integer_arithmetic() {
425425
i ^= i;
426426
}
427427

428+
pub fn issue_10583(a: u16) -> u16 {
429+
10 / a
430+
}
431+
428432
fn main() {}

‎tests/ui/arithmetic_side_effects.stderr

+13-1
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,12 @@ error: arithmetic operation that can potentially result in unexpected side-effec
576576
LL | i * 2;
577577
| ^^^^^
578578

579+
error: arithmetic operation that can potentially result in unexpected side-effects
580+
--> $DIR/arithmetic_side_effects.rs:394:5
581+
|
582+
LL | 1 % i / 2;
583+
| ^^^^^
584+
579585
error: arithmetic operation that can potentially result in unexpected side-effects
580586
--> $DIR/arithmetic_side_effects.rs:395:5
581587
|
@@ -642,5 +648,11 @@ error: arithmetic operation that can potentially result in unexpected side-effec
642648
LL | i %= var2;
643649
| ^^^^^^^^^
644650

645-
error: aborting due to 107 previous errors
651+
error: arithmetic operation that can potentially result in unexpected side-effects
652+
--> $DIR/arithmetic_side_effects.rs:429:5
653+
|
654+
LL | 10 / a
655+
| ^^^^^^
656+
657+
error: aborting due to 109 previous errors
646658

0 commit comments

Comments
 (0)
Please sign in to comment.