@@ -352,8 +352,10 @@ impl<'a> CoverageSpansGenerator<'a> {
352
352
353
353
let prev = self . take_prev ( ) ;
354
354
debug ! ( " AT END, adding last prev={prev:?}" ) ;
355
- let pending_dups = self . pending_dups . split_off ( 0 ) ;
356
- for dup in pending_dups {
355
+
356
+ // Take `pending_dups` so that we can drain it while calling self methods.
357
+ // It is never used as a field after this point.
358
+ for dup in std:: mem:: take ( & mut self . pending_dups ) {
357
359
debug ! ( " ...adding at least one pending dup={:?}" , dup) ;
358
360
self . push_refined_span ( dup) ;
359
361
}
@@ -466,11 +468,16 @@ impl<'a> CoverageSpansGenerator<'a> {
466
468
previous iteration, or prev started a new disjoint span"
467
469
) ;
468
470
if dup. span . hi ( ) <= self . curr ( ) . span . lo ( ) {
469
- let pending_dups = self . pending_dups . split_off ( 0 ) ;
470
- for dup in pending_dups. into_iter ( ) {
471
+ // Temporarily steal `pending_dups` into a local, so that we can
472
+ // drain it while calling other self methods.
473
+ let mut pending_dups = std:: mem:: take ( & mut self . pending_dups ) ;
474
+ for dup in pending_dups. drain ( ..) {
471
475
debug ! ( " ...adding at least one pending={:?}" , dup) ;
472
476
self . push_refined_span ( dup) ;
473
477
}
478
+ // The list of dups is now empty, but we can recycle its capacity.
479
+ self . pending_dups = pending_dups;
480
+ assert ! ( self . pending_dups. is_empty( ) ) ;
474
481
} else {
475
482
self . pending_dups . clear ( ) ;
476
483
}
@@ -519,7 +526,10 @@ impl<'a> CoverageSpansGenerator<'a> {
519
526
let has_pre_closure_span = prev. span . lo ( ) < right_cutoff;
520
527
let has_post_closure_span = prev. span . hi ( ) > right_cutoff;
521
528
522
- let mut pending_dups = self . pending_dups . split_off ( 0 ) ;
529
+ // Temporarily steal `pending_dups` into a local, so that we can
530
+ // mutate and/or drain it while calling other self methods.
531
+ let mut pending_dups = std:: mem:: take ( & mut self . pending_dups ) ;
532
+
523
533
if has_pre_closure_span {
524
534
let mut pre_closure = self . prev ( ) . clone ( ) ;
525
535
pre_closure. span = pre_closure. span . with_hi ( left_cutoff) ;
@@ -533,6 +543,7 @@ impl<'a> CoverageSpansGenerator<'a> {
533
543
}
534
544
self . push_refined_span ( pre_closure) ;
535
545
}
546
+
536
547
if has_post_closure_span {
537
548
// Mutate `prev.span()` to start after the closure (and discard curr).
538
549
// (**NEVER** update `prev_original_span` because it affects the assumptions
@@ -543,12 +554,15 @@ impl<'a> CoverageSpansGenerator<'a> {
543
554
debug ! ( " ...and at least one overlapping dup={:?}" , dup) ;
544
555
dup. span = dup. span . with_lo ( right_cutoff) ;
545
556
}
546
- self . pending_dups . append ( & mut pending_dups) ;
547
557
let closure_covspan = self . take_curr ( ) ; // Prevent this curr from becoming prev.
548
558
self . push_refined_span ( closure_covspan) ; // since self.prev() was already updated
549
559
} else {
550
560
pending_dups. clear ( ) ;
551
561
}
562
+
563
+ // Restore the modified post-closure spans, or the empty vector's capacity.
564
+ assert ! ( self . pending_dups. is_empty( ) ) ;
565
+ self . pending_dups = pending_dups;
552
566
}
553
567
554
568
/// Called if `curr.span` equals `prev_original_span` (and potentially equal to all
0 commit comments