Rollup merge of #88505 - ibraheemdev:use-unwrap-unchecked, r=kennytm

Use `unwrap_unchecked` where possible
This commit is contained in:
Mara Bos 2021-09-02 19:10:14 +02:00 committed by GitHub
commit 8fd1bf3323
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 11 deletions

View File

@ -300,7 +300,10 @@ impl<T> LinkedList<T> {
let tail = self.tail.take(); let tail = self.tail.take();
let len = mem::replace(&mut self.len, 0); let len = mem::replace(&mut self.len, 0);
if let Some(head) = head { if let Some(head) = head {
let tail = tail.unwrap_or_else(|| unsafe { core::hint::unreachable_unchecked() }); // SAFETY: In a LinkedList, either both the head and tail are None because
// the list is empty, or both head and tail are Some because the list is populated.
// Since we have verified the head is Some, we are sure the tail is Some too.
let tail = unsafe { tail.unwrap_unchecked() };
Some((head, tail, len)) Some((head, tail, len))
} else { } else {
None None

View File

@ -459,11 +459,8 @@ where
debug_assert!(N <= iter.size_hint().1.unwrap_or(usize::MAX)); debug_assert!(N <= iter.size_hint().1.unwrap_or(usize::MAX));
debug_assert!(N <= iter.size_hint().0); debug_assert!(N <= iter.size_hint().0);
match collect_into_array(iter) { // SAFETY: covered by the function contract.
Some(array) => array, unsafe { collect_into_array(iter).unwrap_unchecked() }
// SAFETY: covered by the function contract.
None => unsafe { crate::hint::unreachable_unchecked() },
}
} }
/// Pulls `N` items from `iter` and returns them as an array. If the iterator /// Pulls `N` items from `iter` and returns them as an array. If the iterator

View File

@ -1198,11 +1198,8 @@ impl<T> Option<T> {
pub fn insert(&mut self, value: T) -> &mut T { pub fn insert(&mut self, value: T) -> &mut T {
*self = Some(value); *self = Some(value);
match self { // SAFETY: the code above just filled the option
Some(v) => v, unsafe { self.as_mut().unwrap_unchecked() }
// SAFETY: the code above just filled the option
None => unsafe { hint::unreachable_unchecked() },
}
} }
/// Inserts `value` into the option if it is [`None`], then /// Inserts `value` into the option if it is [`None`], then