@@ -185,6 +185,34 @@ impl ArithmeticSideEffects {
185185 }
186186 }
187187
188+ /// There are some integer methods like `wrapping_div` that will panic depending on the
189+ /// provided input.
190+ fn manage_method_call < ' tcx > (
191+ & mut self ,
192+ args : & [ hir:: Expr < ' tcx > ] ,
193+ cx : & LateContext < ' tcx > ,
194+ ps : & hir:: PathSegment < ' tcx > ,
195+ receiver : & hir:: Expr < ' tcx > ,
196+ ) {
197+ const METHODS : & [ & str ] = & [ "saturating_div" , "wrapping_div" , "wrapping_rem" , "wrapping_rem_euclid" ] ;
198+ let Some ( arg) = args. first ( ) else { return ; } ;
199+ if constant_simple ( cx, cx. typeck_results ( ) , receiver) . is_some ( ) {
200+ return ;
201+ }
202+ let instance_ty = cx. typeck_results ( ) . expr_ty ( receiver) ;
203+ if !Self :: is_integral ( instance_ty) {
204+ return ;
205+ }
206+ if METHODS . iter ( ) . copied ( ) . all ( |method| method != ps. ident . as_str ( ) ) {
207+ return ;
208+ }
209+ let ( actual_arg, _) = peel_hir_expr_refs ( arg) ;
210+ match Self :: literal_integer ( cx, actual_arg) {
211+ None | Some ( 0 ) => self . issue_lint ( cx, arg) ,
212+ Some ( _) => { } ,
213+ }
214+ }
215+
188216 fn manage_unary_ops < ' tcx > (
189217 & mut self ,
190218 cx : & LateContext < ' tcx > ,
@@ -224,6 +252,9 @@ impl<'tcx> LateLintPass<'tcx> for ArithmeticSideEffects {
224252 hir:: ExprKind :: AssignOp ( op, lhs, rhs) | hir:: ExprKind :: Binary ( op, lhs, rhs) => {
225253 self . manage_bin_ops ( cx, expr, op, lhs, rhs) ;
226254 } ,
255+ hir:: ExprKind :: MethodCall ( ps, receiver, args, _) => {
256+ self . manage_method_call ( args, cx, ps, receiver) ;
257+ } ,
227258 hir:: ExprKind :: Unary ( un_op, un_expr) => {
228259 self . manage_unary_ops ( cx, expr, un_expr, * un_op) ;
229260 } ,
0 commit comments