@@ -91,35 +91,22 @@ impl ArithmeticSideEffects {
91
91
) -> bool {
92
92
const SATURATING : & [ & str ] = & [ "core" , "num" , "saturating" , "Saturating" ] ;
93
93
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 > | {
95
96
matches ! (
96
97
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 )
109
99
)
110
100
} ;
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 {
113
103
return true ;
114
104
}
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;
123
110
}
124
111
false
125
112
}
0 commit comments