Skip to content

Commit e58a5fb

Browse files
committed
[arithmetic_side_effects] Fix rust-lang#11393
1 parent d802ab2 commit e58a5fb

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

clippy_lints/src/operators/arithmetic_side_effects.rs

+10-23
Original file line numberDiff line numberDiff line change
@@ -91,35 +91,22 @@ impl ArithmeticSideEffects {
9191
) -> bool {
9292
const SATURATING: &[&str] = &["core", "num", "saturating", "Saturating"];
9393
const WRAPPING: &[&str] = &["core", "num", "wrapping", "Wrapping"];
94-
let is_non_zero = |symbol: Option<Symbol>| {
94+
let is_div_or_rem = matches!(op.node, hir::BinOpKind::Div | hir::BinOpKind::Rem);
95+
let is_non_zero_u = |symbol: Option<Symbol>| {
9596
matches!(
9697
symbol,
97-
Some(
98-
sym::NonZeroI128
99-
| sym::NonZeroI16
100-
| sym::NonZeroI32
101-
| sym::NonZeroI64
102-
| sym::NonZeroI8
103-
| sym::NonZeroU128
104-
| sym::NonZeroU16
105-
| sym::NonZeroU32
106-
| sym::NonZeroU64
107-
| sym::NonZeroU8
108-
)
98+
Some(sym::NonZeroU128 | sym::NonZeroU16 | sym::NonZeroU32 | sym::NonZeroU64 | sym::NonZeroU8)
10999
)
110100
};
111-
// If the RHS is NonZero*, then division or module by zero will never occur
112-
if is_non_zero(type_diagnostic_name(cx, rhs_ty)) && let hir::BinOpKind::Div | hir::BinOpKind::Rem = op.node {
101+
// If the RHS is NonZeroU*, then division or module by zero will never occur
102+
if is_non_zero_u(type_diagnostic_name(cx, rhs_ty)) && is_div_or_rem {
113103
return true;
114104
}
115-
// For `Saturation` or `Wrapping` (RHS), all but division and module are allowed.
116-
let is_div_or_rem = matches!(op.node, hir::BinOpKind::Div | hir::BinOpKind::Rem);
117-
if (match_type(cx, rhs_ty, SATURATING) || match_type(cx, rhs_ty, WRAPPING)) && !is_div_or_rem {
118-
return true;
119-
}
120-
// For `Saturation` or `Wrapping` (LHS), everything is allowed
121-
if match_type(cx, lhs_ty, SATURATING) || match_type(cx, lhs_ty, WRAPPING) {
122-
return true;
105+
// `Saturation` and `Wrapping` can overflow if the RHS is zero in a division or module
106+
let is_lhs_sat_or_wrap = match_type(cx, lhs_ty, SATURATING) || match_type(cx, lhs_ty, WRAPPING);
107+
let is_rhs_sat_or_wrap = match_type(cx, rhs_ty, SATURATING) || match_type(cx, rhs_ty, WRAPPING);
108+
if is_lhs_sat_or_wrap || is_rhs_sat_or_wrap {
109+
return !is_div_or_rem;
123110
}
124111
false
125112
}

tests/ui/arithmetic_side_effects.stderr

+13-1
Original file line numberDiff line numberDiff line change
@@ -714,5 +714,17 @@ error: arithmetic operation that can potentially result in unexpected side-effec
714714
LL | unsigned % nonzero_unsigned
715715
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
716716

717-
error: aborting due to 119 previous errors
717+
error: arithmetic operation that can potentially result in unexpected side-effects
718+
--> $DIR/arithmetic_side_effects.rs:512:9
719+
|
720+
LL | x / maybe_zero
721+
| ^^^^^^^^^^^^^^
722+
723+
error: arithmetic operation that can potentially result in unexpected side-effects
724+
--> $DIR/arithmetic_side_effects.rs:516:9
725+
|
726+
LL | x % maybe_zero
727+
| ^^^^^^^^^^^^^^
728+
729+
error: aborting due to 121 previous errors
718730

0 commit comments

Comments
 (0)