diff --git a/crates/common/src/lib.rs b/crates/common/src/lib.rs index 0c1b3d6d7..6a7e3caff 100644 --- a/crates/common/src/lib.rs +++ b/crates/common/src/lib.rs @@ -261,7 +261,10 @@ pub const fn help_section_exists(section: &str) -> bool { #[macro_export] macro_rules! help_section { ($section:expr) => {{ - const _: () = assert!($crate::help_section_exists($section)); + const { + assert!($crate::help_section_exists($section)); + } + $section }}; } diff --git a/crates/fallback/src/lib.rs b/crates/fallback/src/lib.rs index 8165c57e8..d3b53f9fa 100644 --- a/crates/fallback/src/lib.rs +++ b/crates/fallback/src/lib.rs @@ -36,7 +36,10 @@ pub fn wcwidth(c: char) -> isize { pub unsafe fn wcwidth(c: libc::wchar_t) -> libc::c_int; } - const _: () = assert!(std::mem::size_of::() >= std::mem::size_of::()); + const { + assert!(size_of::() >= size_of::()); + } + let width = unsafe { wcwidth(c as libc::wchar_t) }; isize::try_from(width).unwrap() } diff --git a/crates/gettext-mo-file-parser/src/lib.rs b/crates/gettext-mo-file-parser/src/lib.rs index 5c0e4a133..4ce0f7bcf 100644 --- a/crates/gettext-mo-file-parser/src/lib.rs +++ b/crates/gettext-mo-file-parser/src/lib.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -const U32_SIZE: usize = std::mem::size_of::(); +const U32_SIZE: usize = size_of::(); fn read_le_u32(bytes: &[u8]) -> u32 { u32::from_le_bytes(bytes[..U32_SIZE].try_into().unwrap()) @@ -47,9 +47,12 @@ fn check_if_revision_is_supported(revision: u32) -> std::io::Result<()> { } fn as_usize(value: u32) -> usize { - use std::mem::size_of; - const _: () = assert!(size_of::() <= size_of::()); - usize::try_from(value).unwrap() + const { + assert!(size_of::() <= size_of::()); + } + + // SAFETY: `usize` is guaranteed to be at least as wide as `u32` by the const assert above. + unsafe { usize::try_from(value).unwrap_unchecked() } } fn parse_strings( diff --git a/src/builtins/ulimit.rs b/src/builtins/ulimit.rs index d0e3868ef..2639b5430 100644 --- a/src/builtins/ulimit.rs +++ b/src/builtins/ulimit.rs @@ -87,10 +87,9 @@ macro_rules! define_on { fn convert_resource(resource: c_uint) -> ResourceEnum { let resource: i32 = resource.try_into().unwrap(); - use std::mem::{size_of, transmute}; - // Resource is #[repr(i32)] so this is ok - const _: () = assert!(size_of::() == size_of::()); - unsafe { transmute(resource) } + + // SAFETY: Resource is #[repr(i32)] so this is sound + unsafe { std::mem::transmute(resource) } } /// Calls getrlimit. diff --git a/src/proc.rs b/src/proc.rs index 5bac6195a..e05b5c075 100644 --- a/src/proc.rs +++ b/src/proc.rs @@ -148,12 +148,13 @@ pub fn from_exit_code(ret: i32) -> ProcStatus { or invalid builtin exit code!" ); - // Some paranoia. - const _zerocode: i32 = ProcStatus::w_exitcode(0, 0); - const _: () = assert!( - WIFEXITED(_zerocode), - "Synthetic exit status not reported as exited" - ); + const { + let _zerocode = ProcStatus::w_exitcode(0, 0); + assert!( + WIFEXITED(_zerocode), + "Synthetic exit status not reported as exited" + ); + } assert!(ret < 256); ProcStatus::new(Some(Self::w_exitcode(ret, 0 /* sig */))) diff --git a/src/wutil/dir_iter.rs b/src/wutil/dir_iter.rs index 79c69907b..5ba650c8b 100644 --- a/src/wutil/dir_iter.rs +++ b/src/wutil/dir_iter.rs @@ -269,10 +269,9 @@ pub fn next(&mut self) -> Option> { }; // dent.d_name is c_char; pretend it's u8. - assert_eq!( - std::mem::size_of::(), - std::mem::size_of::() - ); + const { + assert!(size_of::() == size_of::()); + } // Do not rely on `libc::dirent::d_name.len()` as dirent names may exceed // the nominal buffer size; instead use the terminating nul byte.