@@ -353,8 +353,10 @@ impl<'a> CoverageSpansGenerator<'a> {
353353
354354 let prev = self . take_prev ( ) ;
355355 debug ! ( " AT END, adding last prev={prev:?}" ) ;
356- let pending_dups = self . pending_dups . split_off ( 0 ) ;
357- for dup in pending_dups {
356+
357+ // Take `pending_dups` so that we can drain it while calling self methods.
358+ // It is never used as a field after this point.
359+ for dup in std:: mem:: take ( & mut self . pending_dups ) {
358360 debug ! ( " ...adding at least one pending dup={:?}" , dup) ;
359361 self . push_refined_span ( dup) ;
360362 }
@@ -473,11 +475,16 @@ impl<'a> CoverageSpansGenerator<'a> {
473475 previous iteration, or prev started a new disjoint span"
474476 ) ;
475477 if dup. span . hi ( ) <= self . curr ( ) . span . lo ( ) {
476- let pending_dups = self . pending_dups . split_off ( 0 ) ;
477- for dup in pending_dups. into_iter ( ) {
478+ // Temporarily steal `pending_dups` into a local, so that we can
479+ // drain it while calling other self methods.
480+ let mut pending_dups = std:: mem:: take ( & mut self . pending_dups ) ;
481+ for dup in pending_dups. drain ( ..) {
478482 debug ! ( " ...adding at least one pending={:?}" , dup) ;
479483 self . push_refined_span ( dup) ;
480484 }
485+ // The list of dups is now empty, but we can recycle its capacity.
486+ assert ! ( pending_dups. is_empty( ) && self . pending_dups. is_empty( ) ) ;
487+ self . pending_dups = pending_dups;
481488 } else {
482489 self . pending_dups . clear ( ) ;
483490 }
@@ -526,7 +533,10 @@ impl<'a> CoverageSpansGenerator<'a> {
526533 let has_pre_closure_span = prev. span . lo ( ) < right_cutoff;
527534 let has_post_closure_span = prev. span . hi ( ) > right_cutoff;
528535
529- let mut pending_dups = self . pending_dups . split_off ( 0 ) ;
536+ // Temporarily steal `pending_dups` into a local, so that we can
537+ // mutate and/or drain it while calling other self methods.
538+ let mut pending_dups = std:: mem:: take ( & mut self . pending_dups ) ;
539+
530540 if has_pre_closure_span {
531541 let mut pre_closure = self . prev ( ) . clone ( ) ;
532542 pre_closure. span = pre_closure. span . with_hi ( left_cutoff) ;
@@ -540,6 +550,7 @@ impl<'a> CoverageSpansGenerator<'a> {
540550 }
541551 self . push_refined_span ( pre_closure) ;
542552 }
553+
543554 if has_post_closure_span {
544555 // Mutate `prev.span()` to start after the closure (and discard curr).
545556 // (**NEVER** update `prev_original_span` because it affects the assumptions
@@ -550,12 +561,15 @@ impl<'a> CoverageSpansGenerator<'a> {
550561 debug ! ( " ...and at least one overlapping dup={:?}" , dup) ;
551562 dup. span = dup. span . with_lo ( right_cutoff) ;
552563 }
553- self . pending_dups . append ( & mut pending_dups) ;
554564 let closure_covspan = self . take_curr ( ) ; // Prevent this curr from becoming prev.
555565 self . push_refined_span ( closure_covspan) ; // since self.prev() was already updated
556566 } else {
557567 pending_dups. clear ( ) ;
558568 }
569+
570+ // Restore the modified post-closure spans, or the empty vector's capacity.
571+ assert ! ( self . pending_dups. is_empty( ) ) ;
572+ self . pending_dups = pending_dups;
559573 }
560574
561575 /// Called if `curr.span` equals `prev_original_span` (and potentially equal to all
0 commit comments