Rust

Rust

Made by DeepSource

Found transmute between integer literal and fn ptr RS-E1027

Bug risk
Major

Creating a null function pointer is undefined behavior. Certain Rust types are defined to never be null. This includes references (&T, &mut T), boxes (Box), and function pointers (extern "abi" fn()).

When interfacing with C, pointers that might be null are often used, possibly requiring some convoluted transmutes and/or unsafe code to handle conversions to or from Rust types.

However, trying to construct or work with these invalid values is undefined behavior.

Bad practice

fn foo() {
    let null_fn: fn() = unsafe { std::mem::transmute( std::ptr::null::<()>() ) };
}

Recommended

fn foo() {
    let null_fn: Option<fn()> = None;
}

References