@@ -564,20 +564,45 @@ pub fn trait_ref_of_method<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Optio
564
564
565
565
/// Checks if the top level expression can be moved into a closure as is.
566
566
/// Currently checks for:
567
- /// * Break/Continue outside the given jump targets
567
+ /// * Break/Continue outside the given loop HIR ids.
568
568
/// * Yield/Return statments.
569
- /// * Inline assembly
569
+ /// * Inline assembly.
570
570
/// * Usages of a field of a local where the type of the local can be partially moved.
571
+ ///
572
+ /// For example, given the following function:
573
+ ///
574
+ /// ```
575
+ /// fn f<'a>(iter: &mut impl Iterator<Item = (usize, &'a mut String)>) {
576
+ /// for item in iter {
577
+ /// let s = item.1;
578
+ /// if item.0 > 10 {
579
+ /// continue;
580
+ /// } else {
581
+ /// s.clear();
582
+ /// }
583
+ /// }
584
+ /// }
585
+ /// ```
586
+ ///
587
+ /// When called on the expression `item.0` this will return false unless the local `item` is in the
588
+ /// `ignore_locals` set. The type `(usize, &mut String)` can have the second element moved, so it
589
+ /// isn't always safe to move into a closure when only a single field is needed.
590
+ ///
591
+ /// When called on the `continue` expression this will return false unless the outer loop expression
592
+ /// is in the `loop_ids` set.
593
+ ///
594
+ /// Note that this check is not recursive, so passing the `if` expression will always return true
595
+ /// even though sub-expressions might return false.
571
596
pub fn can_move_expr_to_closure_no_visit (
572
597
cx : & LateContext < ' tcx > ,
573
598
expr : & ' tcx Expr < ' _ > ,
574
- jump_targets : & [ HirId ] ,
599
+ loop_ids : & [ HirId ] ,
575
600
ignore_locals : & HirIdSet ,
576
601
) -> bool {
577
602
match expr. kind {
578
603
ExprKind :: Break ( Destination { target_id : Ok ( id) , .. } , _)
579
604
| ExprKind :: Continue ( Destination { target_id : Ok ( id) , .. } )
580
- if jump_targets . contains ( & id) =>
605
+ if loop_ids . contains ( & id) =>
581
606
{
582
607
true
583
608
} ,
0 commit comments