Skip to content

Commit 531eb6c

Browse files
committed
Improve doc for can_move_expr_to_closure_no_visit
1 parent 219a715 commit 531eb6c

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

clippy_utils/src/lib.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -564,20 +564,45 @@ pub fn trait_ref_of_method<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Optio
564564

565565
/// Checks if the top level expression can be moved into a closure as is.
566566
/// Currently checks for:
567-
/// * Break/Continue outside the given jump targets
567+
/// * Break/Continue outside the given loop HIR ids.
568568
/// * Yield/Return statments.
569-
/// * Inline assembly
569+
/// * Inline assembly.
570570
/// * 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.
571596
pub fn can_move_expr_to_closure_no_visit(
572597
cx: &LateContext<'tcx>,
573598
expr: &'tcx Expr<'_>,
574-
jump_targets: &[HirId],
599+
loop_ids: &[HirId],
575600
ignore_locals: &HirIdSet,
576601
) -> bool {
577602
match expr.kind {
578603
ExprKind::Break(Destination { target_id: Ok(id), .. }, _)
579604
| ExprKind::Continue(Destination { target_id: Ok(id), .. })
580-
if jump_targets.contains(&id) =>
605+
if loop_ids.contains(&id) =>
581606
{
582607
true
583608
},

0 commit comments

Comments
 (0)