From 7e678869ec76404f6192ff3aa2a58a5eefecdcca Mon Sep 17 00:00:00 2001 From: Jade Ellis Date: Sat, 21 Nov 2026 02:14:25 +0000 Subject: [PATCH] refactor(core): Remove unneeded unsafe in UnwrapInfallible and Unquoted --- src/core/utils/result/unwrap_infallible.rs | 8 +++----- src/core/utils/string/unquoted.rs | 9 +++++---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/core/utils/result/unwrap_infallible.rs b/src/core/utils/result/unwrap_infallible.rs index 99309e025..0baf87b66 100644 --- a/src/core/utils/result/unwrap_infallible.rs +++ b/src/core/utils/result/unwrap_infallible.rs @@ -1,7 +1,6 @@ use std::convert::Infallible; -use super::{DebugInspect, Result}; -use crate::error; +use super::Result; pub trait UnwrapInfallible { fn unwrap_infallible(self) -> T; @@ -10,8 +9,7 @@ pub trait UnwrapInfallible { impl UnwrapInfallible for Result { #[inline] fn unwrap_infallible(self) -> T { - // SAFETY: Branchless unwrap for errors that can never happen. In debug - // mode this is asserted. - unsafe { self.debug_inspect_err(error::infallible).unwrap_unchecked() } + let Ok(val) = self; + val } } diff --git a/src/core/utils/string/unquoted.rs b/src/core/utils/string/unquoted.rs index abf7bbc16..a23f49539 100644 --- a/src/core/utils/string/unquoted.rs +++ b/src/core/utils/string/unquoted.rs @@ -32,11 +32,12 @@ impl<'a> From<&'a str> for &'a Unquoted { fn from(s: &'a str) -> &'a Unquoted { let s: &'a str = s.unquote_infallible(); - //SAFETY: This is a pattern I lifted from ruma-identifiers for - // strong-type strs by wrapping in a tuple-struct. - #[allow(clippy::transmute_ptr_to_ptr)] + // SAFETY: `Unquoted` is `#[repr(transparent)]` over `str`, so + // `*const str` and `*const Unquoted` have identical layout and + // metadata, and the returned reference preserves lifetime `'a`. + #[allow(clippy::as_conversions)] unsafe { - std::mem::transmute(s) + &*(std::ptr::from_ref::(s) as *const Unquoted) } } }