about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJiahao XU <Jiahao_XU@outlook.com>2022-06-23 12:58:33 +1000
committerJiahao XU <Jiahao_XU@outlook.com>2022-06-23 12:58:33 +1000
commit516da4c93f804f27b1a5f664ae06b9054532f095 (patch)
tree20ecbb30003e644c6c6ede72a8550429637c4651
parentd2211c9fdc8fd49bdbd17e92a16a9f304eda1d8c (diff)
Use `unwrap` instead of `unwrap_unchecked`
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
-rw-r--r--library/std/src/io/error.rs10
1 files changed, 7 insertions, 3 deletions
diff --git a/library/std/src/io/error.rs b/library/std/src/io/error.rs
index b1931a52290..4c6fd55ad13 100644
--- a/library/std/src/io/error.rs
+++ b/library/std/src/io/error.rs
@@ -844,9 +844,13 @@ impl Error {
             ErrorData::Custom(b) if b.error.is::<E>() => {
                 let res = (*b).error.downcast::<E>();
 
-                // Safety: b.error.is::<E>() returns true,
-                // which means that res must be Ok(e).
-                Ok(unsafe { res.unwrap_unchecked() })
+                // downcast is a really trivial and is marked as inline, so
+                // it's likely be inlined here.
+                //
+                // And the compiler should be able to eliminate the branch
+                // that produces `Err` here since b.error.is::<E>()
+                // returns true.
+                Ok(res.unwrap())
             }
             repr_data => Err(Self { repr: Repr::new(repr_data) }),
         }