Skip to content

Commit

Permalink
[arithmetic_side_effects] Fix #10252
Browse files Browse the repository at this point in the history
  • Loading branch information
c410-f3r committed Feb 12, 2023
1 parent 6f353fd commit c439373
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 123 deletions.
13 changes: 10 additions & 3 deletions clippy_lints/src/operators/arithmetic_side_effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ impl ArithmeticSideEffects {
return;
}
let has_valid_op = if Self::is_integral(lhs_ty) && Self::is_integral(rhs_ty) {
if let hir::BinOpKind::Shl | hir::BinOpKind::Shr = op.node {
// At least for integers, shifts are already handled by the CTFE
return;
}
let (actual_lhs, lhs_ref_counter) = peel_hir_expr_refs(lhs);
let (actual_rhs, rhs_ref_counter) = peel_hir_expr_refs(rhs);
match (
Expand All @@ -151,10 +155,13 @@ impl ArithmeticSideEffects {
) {
(None, None) => false,
(None, Some(n)) | (Some(n), None) => match (&op.node, n) {
(hir::BinOpKind::Div | hir::BinOpKind::Rem, 0) => false,
// 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
(hir::BinOpKind::Add | hir::BinOpKind::Sub, 0)
| (hir::BinOpKind::Div | hir::BinOpKind::Rem, _)
| (hir::BinOpKind::Mul, 0 | 1) => true,
// Multiplication by 1 or 0 will never overflow
| (hir::BinOpKind::Mul, 0 | 1)
=> true,
_ => false,
},
(Some(_), Some(_)) => {
Expand Down
28 changes: 28 additions & 0 deletions tests/ui/arithmetic_side_effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,32 @@ impl_arith!(
Div, Custom, Custom, div;
Mul, Custom, Custom, mul;
Rem, Custom, Custom, rem;
Shl, Custom, Custom, shl;
Shr, Custom, Custom, shr;
Sub, Custom, Custom, sub;

Add, Custom, &Custom, add;
Div, Custom, &Custom, div;
Mul, Custom, &Custom, mul;
Rem, Custom, &Custom, rem;
Shl, Custom, &Custom, shl;
Shr, Custom, &Custom, shr;
Sub, Custom, &Custom, sub;

Add, &Custom, Custom, add;
Div, &Custom, Custom, div;
Mul, &Custom, Custom, mul;
Rem, &Custom, Custom, rem;
Shl, &Custom, Custom, shl;
Shr, &Custom, Custom, shr;
Sub, &Custom, Custom, sub;

Add, &Custom, &Custom, add;
Div, &Custom, &Custom, div;
Mul, &Custom, &Custom, mul;
Rem, &Custom, &Custom, rem;
Shl, &Custom, &Custom, shl;
Shr, &Custom, &Custom, shr;
Sub, &Custom, &Custom, sub;
);

Expand All @@ -71,24 +79,32 @@ impl_assign_arith!(
DivAssign, Custom, Custom, div_assign;
MulAssign, Custom, Custom, mul_assign;
RemAssign, Custom, Custom, rem_assign;
ShlAssign, Custom, Custom, shl_assign;
ShrAssign, Custom, Custom, shr_assign;
SubAssign, Custom, Custom, sub_assign;

AddAssign, Custom, &Custom, add_assign;
DivAssign, Custom, &Custom, div_assign;
MulAssign, Custom, &Custom, mul_assign;
RemAssign, Custom, &Custom, rem_assign;
ShlAssign, Custom, &Custom, shl_assign;
ShrAssign, Custom, &Custom, shr_assign;
SubAssign, Custom, &Custom, sub_assign;

AddAssign, &Custom, Custom, add_assign;
DivAssign, &Custom, Custom, div_assign;
MulAssign, &Custom, Custom, mul_assign;
RemAssign, &Custom, Custom, rem_assign;
ShlAssign, &Custom, Custom, shl_assign;
ShrAssign, &Custom, Custom, shr_assign;
SubAssign, &Custom, Custom, sub_assign;

AddAssign, &Custom, &Custom, add_assign;
DivAssign, &Custom, &Custom, div_assign;
MulAssign, &Custom, &Custom, mul_assign;
RemAssign, &Custom, &Custom, rem_assign;
ShlAssign, &Custom, &Custom, shl_assign;
ShrAssign, &Custom, &Custom, shr_assign;
SubAssign, &Custom, &Custom, sub_assign;
);

Expand Down Expand Up @@ -297,6 +313,10 @@ pub fn unknown_ops_or_runtime_ops_that_can_overflow() {
_custom %= &Custom;
_custom *= Custom;
_custom *= &Custom;
_custom >>= Custom;
_custom >>= &Custom;
_custom <<= Custom;
_custom <<= &Custom;
_custom += -Custom;
_custom += &-Custom;
_custom -= -Custom;
Expand All @@ -307,6 +327,10 @@ pub fn unknown_ops_or_runtime_ops_that_can_overflow() {
_custom %= &-Custom;
_custom *= -Custom;
_custom *= &-Custom;
_custom >>= -Custom;
_custom >>= &-Custom;
_custom <<= -Custom;
_custom <<= &-Custom;

// Binary
_n = _n + 1;
Expand Down Expand Up @@ -347,6 +371,10 @@ pub fn unknown_ops_or_runtime_ops_that_can_overflow() {
_custom = Custom + &Custom;
_custom = &Custom + Custom;
_custom = &Custom + &Custom;
_custom = _custom >> _custom;
_custom = _custom >> &_custom;
_custom = Custom << _custom;
_custom = &Custom << _custom;

// Unary
_n = -_n;
Expand Down
Loading

0 comments on commit c439373

Please sign in to comment.