Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[arithmetic_side_effects] Fix #11393 #11410

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 11 additions & 23 deletions clippy_lints/src/operators/arithmetic_side_effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,36 +91,24 @@ impl ArithmeticSideEffects {
) -> bool {
const SATURATING: &[&str] = &["core", "num", "saturating", "Saturating"];
const WRAPPING: &[&str] = &["core", "num", "wrapping", "Wrapping"];
let is_non_zero = |symbol: Option<Symbol>| {
let is_div_or_rem = matches!(op.node, hir::BinOpKind::Div | hir::BinOpKind::Rem);
let is_non_zero_u = |symbol: Option<Symbol>| {
matches!(
symbol,
Some(
sym::NonZeroI128
| sym::NonZeroI16
| sym::NonZeroI32
| sym::NonZeroI64
| sym::NonZeroI8
| sym::NonZeroU128
| sym::NonZeroU16
| sym::NonZeroU32
| sym::NonZeroU64
| sym::NonZeroU8
)
Some(sym::NonZeroU128 | sym::NonZeroU16 | sym::NonZeroU32 | sym::NonZeroU64 | sym::NonZeroU8)
)
};
// If the RHS is NonZero*, then division or module by zero will never occur
if is_non_zero(type_diagnostic_name(cx, rhs_ty)) && let hir::BinOpKind::Div | hir::BinOpKind::Rem = op.node {
return true;
}
// For `Saturation` or `Wrapping` (RHS), all but division and module are allowed.
let is_div_or_rem = matches!(op.node, hir::BinOpKind::Div | hir::BinOpKind::Rem);
if (match_type(cx, rhs_ty, SATURATING) || match_type(cx, rhs_ty, WRAPPING)) && !is_div_or_rem {
let is_sat_or_wrap = |ty: Ty<'_>| match_type(cx, ty, SATURATING) || match_type(cx, ty, WRAPPING);

// If the RHS is NonZeroU*, then division or module by zero will never occur
if is_non_zero_u(type_diagnostic_name(cx, rhs_ty)) && is_div_or_rem {
return true;
}
// For `Saturation` or `Wrapping` (LHS), everything is allowed
if match_type(cx, lhs_ty, SATURATING) || match_type(cx, lhs_ty, WRAPPING) {
return true;
// `Saturation` and `Wrapping` can overflow if the RHS is zero in a division or module
if is_sat_or_wrap(lhs_ty) {
return !is_div_or_rem;
}

false
}

Expand Down
14 changes: 13 additions & 1 deletion tests/ui/arithmetic_side_effects.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -714,5 +714,17 @@ error: arithmetic operation that can potentially result in unexpected side-effec
LL | unsigned % nonzero_unsigned
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 119 previous errors
error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:512:9
|
LL | x / maybe_zero
| ^^^^^^^^^^^^^^

error: arithmetic operation that can potentially result in unexpected side-effects
--> $DIR/arithmetic_side_effects.rs:516:9
|
LL | x % maybe_zero
| ^^^^^^^^^^^^^^

error: aborting due to 121 previous errors