Skip to content

Commit 1c6fc6d

Browse files
committed
Avoid emptyness check in PeekMut::pop
1 parent 9c7013c commit 1c6fc6d

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

library/alloc/src/collections/binary_heap/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,10 @@ impl<'a, T: Ord, A: Allocator> PeekMut<'a, T, A> {
374374
// the caller could've mutated the element. It is removed from the
375375
// heap on the next line and pop() is not sensitive to its value.
376376
}
377-
this.heap.pop().unwrap()
377+
378+
// SAFETY: Have a `PeekMut` element proves that the associated binary heap being non-empty,
379+
// so the `pop` operation will not fail.
380+
unsafe { this.heap.pop().unwrap_unchecked() }
378381
}
379382
}
380383

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@ compile-flags: -O
2+
#![crate_type = "lib"]
3+
4+
use std::collections::binary_heap::PeekMut;
5+
6+
// CHECK-LABEL: @peek_mut_pop
7+
#[no_mangle]
8+
pub fn peek_mut_pop(peek_mut: PeekMut<u32>) -> u32 {
9+
// CHECK-NOT: call {{.*}}drop_in_place
10+
// CHECK-NOT: unwrap_failed
11+
PeekMut::pop(peek_mut)
12+
}

0 commit comments

Comments
 (0)