about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMara Bos <m-ou.se@m-ou.se>2021-09-02 19:10:14 +0200
committerGitHub <noreply@github.com>2021-09-02 19:10:14 +0200
commit8fd1bf332350b5899f040d749fa2bcc21ac4e23e (patch)
treef45961ad80e16fdb8740481bf1969c27355bfdd6
parente50069ff4f4b8c95443103d747f6aa22e64dc22c (diff)
parentffc43b84682b9378a03b4cf5b166b2049625f7a4 (diff)
downloadrust-8fd1bf332350b5899f040d749fa2bcc21ac4e23e.tar.gz
rust-8fd1bf332350b5899f040d749fa2bcc21ac4e23e.zip
Rollup merge of #88505 - ibraheemdev:use-unwrap-unchecked, r=kennytm
Use `unwrap_unchecked` where possible
-rw-r--r--library/alloc/src/collections/linked_list.rs5
-rw-r--r--library/core/src/array/mod.rs7
-rw-r--r--library/core/src/option.rs7
3 files changed, 8 insertions, 11 deletions
diff --git a/library/alloc/src/collections/linked_list.rs b/library/alloc/src/collections/linked_list.rs
index 7aa24ff4afa..9d45c5082db 100644
--- a/library/alloc/src/collections/linked_list.rs
+++ b/library/alloc/src/collections/linked_list.rs
@@ -300,7 +300,10 @@ 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() });
+            // 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))
         } else {
             None
diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs
index 3c638e655dc..70cccd31b92 100644
--- a/library/core/src/array/mod.rs
+++ b/library/core/src/array/mod.rs
@@ -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
diff --git a/library/core/src/option.rs b/library/core/src/option.rs
index 47865240f6a..9d5e03dd0de 100644
--- a/library/core/src/option.rs
+++ b/library/core/src/option.rs
@@ -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