Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use unwrap_unchecked where possible #88505

Merged
merged 2 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/alloc/src/collections/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ impl<T> LinkedList<T> {
let tail = self.tail.take();
let len = mem::replace(&mut self.len, 0);
if let Some(head) = head {
let tail = tail.unwrap_or_else(|| unsafe { core::hint::unreachable_unchecked() });
let tail = unsafe { tail.unwrap_unchecked() };
ibraheemdev marked this conversation as resolved.
Show resolved Hide resolved
Some((head, tail, len))
} else {
None
Expand Down
7 changes: 2 additions & 5 deletions library/core/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,11 +459,8 @@ where
debug_assert!(N <= iter.size_hint().1.unwrap_or(usize::MAX));
debug_assert!(N <= iter.size_hint().0);

match collect_into_array(iter) {
Some(array) => array,
// SAFETY: covered by the function contract.
None => unsafe { crate::hint::unreachable_unchecked() },
}
// SAFETY: covered by the function contract.
unsafe { collect_into_array(iter).unwrap_unchecked() }
}

/// Pulls `N` items from `iter` and returns them as an array. If the iterator
Expand Down
7 changes: 2 additions & 5 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1198,11 +1198,8 @@ impl<T> Option<T> {
pub fn insert(&mut self, value: T) -> &mut T {
*self = Some(value);

match self {
Some(v) => v,
// SAFETY: the code above just filled the option
None => unsafe { hint::unreachable_unchecked() },
}
// SAFETY: the code above just filled the option
unsafe { self.as_mut().unwrap_unchecked() }
}

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