Skip to content

Commit 8fd1bf3

Browse files
authored
Rollup merge of #88505 - ibraheemdev:use-unwrap-unchecked, r=kennytm
Use `unwrap_unchecked` where possible
2 parents e50069f + ffc43b8 commit 8fd1bf3

File tree

3 files changed

+8
-11
lines changed

3 files changed

+8
-11
lines changed

library/alloc/src/collections/linked_list.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,10 @@ impl<T> LinkedList<T> {
300300
let tail = self.tail.take();
301301
let len = mem::replace(&mut self.len, 0);
302302
if let Some(head) = head {
303-
let tail = tail.unwrap_or_else(|| unsafe { core::hint::unreachable_unchecked() });
303+
// SAFETY: In a LinkedList, either both the head and tail are None because
304+
// the list is empty, or both head and tail are Some because the list is populated.
305+
// Since we have verified the head is Some, we are sure the tail is Some too.
306+
let tail = unsafe { tail.unwrap_unchecked() };
304307
Some((head, tail, len))
305308
} else {
306309
None

library/core/src/array/mod.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -459,11 +459,8 @@ where
459459
debug_assert!(N <= iter.size_hint().1.unwrap_or(usize::MAX));
460460
debug_assert!(N <= iter.size_hint().0);
461461

462-
match collect_into_array(iter) {
463-
Some(array) => array,
464-
// SAFETY: covered by the function contract.
465-
None => unsafe { crate::hint::unreachable_unchecked() },
466-
}
462+
// SAFETY: covered by the function contract.
463+
unsafe { collect_into_array(iter).unwrap_unchecked() }
467464
}
468465

469466
/// Pulls `N` items from `iter` and returns them as an array. If the iterator

library/core/src/option.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1198,11 +1198,8 @@ impl<T> Option<T> {
11981198
pub fn insert(&mut self, value: T) -> &mut T {
11991199
*self = Some(value);
12001200

1201-
match self {
1202-
Some(v) => v,
1203-
// SAFETY: the code above just filled the option
1204-
None => unsafe { hint::unreachable_unchecked() },
1205-
}
1201+
// SAFETY: the code above just filled the option
1202+
unsafe { self.as_mut().unwrap_unchecked() }
12061203
}
12071204

12081205
/// Inserts `value` into the option if it is [`None`], then

0 commit comments

Comments
 (0)