Skip to content

Commit 516da4c

Browse files
committed
Use unwrap instead of unwrap_unchecked
Signed-off-by: Jiahao XU <[email protected]>
1 parent d2211c9 commit 516da4c

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

library/std/src/io/error.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -844,9 +844,13 @@ impl Error {
844844
ErrorData::Custom(b) if b.error.is::<E>() => {
845845
let res = (*b).error.downcast::<E>();
846846

847-
// Safety: b.error.is::<E>() returns true,
848-
// which means that res must be Ok(e).
849-
Ok(unsafe { res.unwrap_unchecked() })
847+
// downcast is a really trivial and is marked as inline, so
848+
// it's likely be inlined here.
849+
//
850+
// And the compiler should be able to eliminate the branch
851+
// that produces `Err` here since b.error.is::<E>()
852+
// returns true.
853+
Ok(res.unwrap())
850854
}
851855
repr_data => Err(Self { repr: Repr::new(repr_data) }),
852856
}

0 commit comments

Comments
 (0)