@@ -184,6 +184,34 @@ impl ArithmeticSideEffects {
184
184
}
185
185
}
186
186
187
+ /// There are some integer methods like `wrapping_div` that will panic depending on the
188
+ /// provided input.
189
+ fn manage_method_call < ' tcx > (
190
+ & mut self ,
191
+ args : & [ hir:: Expr < ' tcx > ] ,
192
+ cx : & LateContext < ' tcx > ,
193
+ instance : & hir:: Expr < ' tcx > ,
194
+ ps : & hir:: PathSegment < ' tcx > ,
195
+ ) {
196
+ const METHODS : & [ & str ] = & [ "wrapping_div" , "wrapping_rem" , "wrapping_rem_euclid" ] ;
197
+ let Some ( arg) = args. first ( ) else { return ; } ;
198
+ if constant_simple ( cx, cx. typeck_results ( ) , instance) . is_some ( ) {
199
+ return ;
200
+ }
201
+ let instance_ty = cx. typeck_results ( ) . expr_ty ( instance) ;
202
+ if !Self :: is_integral ( instance_ty) {
203
+ return ;
204
+ }
205
+ let has_method = METHODS . iter ( ) . copied ( ) . any ( |method| method == ps. ident . as_str ( ) ) ;
206
+ if !has_method {
207
+ return ;
208
+ }
209
+ let ( actual_arg, _) = peel_hir_expr_refs ( arg) ;
210
+ if let Some ( n) = Self :: literal_integer ( cx, actual_arg) && n == 0 {
211
+ self . issue_lint ( cx, arg) ;
212
+ }
213
+ }
214
+
187
215
fn manage_unary_ops < ' tcx > (
188
216
& mut self ,
189
217
cx : & LateContext < ' tcx > ,
@@ -222,6 +250,9 @@ impl<'tcx> LateLintPass<'tcx> for ArithmeticSideEffects {
222
250
hir:: ExprKind :: AssignOp ( op, lhs, rhs) | hir:: ExprKind :: Binary ( op, lhs, rhs) => {
223
251
self . manage_bin_ops ( cx, expr, op, lhs, rhs) ;
224
252
} ,
253
+ hir:: ExprKind :: MethodCall ( ps, instance, args, _) => {
254
+ self . manage_method_call ( args, cx, instance, ps) ;
255
+ } ,
225
256
hir:: ExprKind :: Unary ( un_op, un_expr) => {
226
257
self . manage_unary_ops ( cx, expr, un_expr, * un_op) ;
227
258
} ,
0 commit comments