@@ -1377,33 +1377,40 @@ pub fn option_arg_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'t
1377
1377
}
1378
1378
}
1379
1379
1380
- /// Check if `ty` is an `Iterator` and has side effects when iterated over. Currently, this only
1381
- /// checks if the `ty` contains mutable captures, and thus may be imcomplete .
1380
+ /// Check if a Ty<'_> of `Iterator` has side effects when iterated over by checking if it
1381
+ /// captures any mutable references or equivalents .
1382
1382
pub fn is_iter_with_side_effects < ' tcx > ( cx : & LateContext < ' tcx > , iter_ty : Ty < ' tcx > ) -> bool {
1383
- let Some ( iter_trait) = cx. tcx . lang_items ( ) . iterator_trait ( ) else {
1384
- return false ;
1385
- } ;
1386
-
1387
- is_iter_with_side_effects_impl ( cx, iter_ty, iter_trait)
1383
+ cx. tcx
1384
+ . lang_items ( )
1385
+ . iterator_trait ( )
1386
+ . is_some_and ( |iter_trait| has_non_owning_mutable_access ( cx, iter_ty, iter_trait) )
1388
1387
}
1389
1388
1390
- fn is_iter_with_side_effects_impl < ' tcx > ( cx : & LateContext < ' tcx > , iter_ty : Ty < ' tcx > , iter_trait : DefId ) -> bool {
1391
- if implements_trait ( cx, iter_ty, iter_trait, & [ ] )
1392
- && let ty:: Adt ( _, args) = iter_ty. kind ( )
1393
- {
1394
- return args. types ( ) . any ( |arg_ty| {
1395
- if let ty:: Closure ( _, closure_args) = arg_ty. kind ( )
1396
- && let Some ( captures) = closure_args. types ( ) . next_back ( )
1397
- {
1398
- captures
1399
- . tuple_fields ( )
1400
- . iter ( )
1401
- . any ( |capture_ty| matches ! ( capture_ty. ref_mutability( ) , Some ( Mutability :: Mut ) ) )
1402
- } else {
1403
- is_iter_with_side_effects_impl ( cx, arg_ty, iter_trait)
1404
- }
1405
- } ) ;
1389
+ /// Check if `ty` contains mutable references or equivalent, which includes:
1390
+ /// - A mutable reference/pointer.
1391
+ /// - A reference/pointer to a non-`Freeze` type.
1392
+ /// - A `PhantomData` type containing any of the previous.
1393
+ fn has_non_owning_mutable_access < ' tcx > ( cx : & LateContext < ' tcx > , ty : Ty < ' tcx > , iter_trait : DefId ) -> bool {
1394
+ match ty. kind ( ) {
1395
+ ty:: Adt ( adt_def, args) if adt_def. is_phantom_data ( ) => args
1396
+ . types ( )
1397
+ . any ( |arg_ty| has_non_owning_mutable_access ( cx, arg_ty, iter_trait) ) ,
1398
+ ty:: Adt ( adt_def, args) => adt_def
1399
+ . all_fields ( )
1400
+ . any ( |field| has_non_owning_mutable_access ( cx, field. ty ( cx. tcx , args) , iter_trait) ) ,
1401
+ ty:: Array ( elem_ty, _) | ty:: Slice ( elem_ty) => has_non_owning_mutable_access ( cx, * elem_ty, iter_trait) ,
1402
+ ty:: Ref ( _, pointee_ty, Mutability :: Mut ) if implements_trait ( cx, * pointee_ty, iter_trait, & [ ] ) => {
1403
+ has_non_owning_mutable_access ( cx, * pointee_ty, iter_trait)
1404
+ } ,
1405
+ ty:: RawPtr ( pointee_ty, mutability) | ty:: Ref ( _, pointee_ty, mutability) => {
1406
+ mutability. is_mut ( ) || !pointee_ty. is_freeze ( cx. tcx , cx. typing_env ( ) )
1407
+ } ,
1408
+ ty:: Closure ( _, closure_args) => {
1409
+ matches ! ( closure_args. types( ) . next_back( ) , Some ( captures) if has_non_owning_mutable_access( cx, captures, iter_trait) )
1410
+ } ,
1411
+ ty:: Tuple ( tuple_args) => tuple_args
1412
+ . iter ( )
1413
+ . any ( |arg_ty| has_non_owning_mutable_access ( cx, arg_ty, iter_trait) ) ,
1414
+ _ => false ,
1406
1415
}
1407
-
1408
- false
1409
1416
}
0 commit comments