Rust

Rust

Made by DeepSource

Found transmute between an integer and NonZero type RS-E1026

Bug risk
Major

Transmutes from integers to NonZero* types can be unsound. Transmutes work on any type provided and might cause unsoundness when those types change elsewhere. The new_unchecked method only works for the appropriate types instead, restricting the transmutes to compatible integer conversions.

Consider using the new_unchecked method instead.

Bad practice

fn foo() {
    let _non_zero: NonZeroU32 = unsafe { std::mem::transmute(123) };
}

Recommended

fn foo() {
    let _non_zero = unsafe { NonZeroU32::new_unchecked(123) };
}