From 52ed89ae8c264d8885bfda4f79033289db459c02 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 28 May 2020 10:31:38 +0200 Subject: [PATCH] from_u32_unchecked: check validity when debug assertions are enabled --- src/libcore/char/convert.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcore/char/convert.rs b/src/libcore/char/convert.rs index 315020bac585..87c56c4b0a10 100644 --- a/src/libcore/char/convert.rs +++ b/src/libcore/char/convert.rs @@ -99,7 +99,7 @@ pub fn from_u32(i: u32) -> Option { #[inline] #[stable(feature = "char_from_unchecked", since = "1.5.0")] pub unsafe fn from_u32_unchecked(i: u32) -> char { - transmute(i) + if cfg!(debug_assertions) { char::from_u32(i).unwrap() } else { transmute(i) } } #[stable(feature = "char_convert", since = "1.13.0")] @@ -218,7 +218,7 @@ impl TryFrom for char { Err(CharTryFromError(())) } else { // SAFETY: checked that it's a legal unicode value - Ok(unsafe { from_u32_unchecked(i) }) + Ok(unsafe { transmute(i) }) } } }