Skip to content

Commit d47e388

Browse files
committed
Avoid emptiness check in PeekMut::pop
1 parent 9c7013c commit d47e388

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

Diff for: 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

Diff for: tests/codegen/binary-heap-peek-mut-pop-no-panic.rs

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

0 commit comments

Comments
 (0)