mod transmute_int_to_bool;
mod transmute_int_to_char;
mod transmute_int_to_float;
+mod transmute_null_to_fn;
mod transmute_num_to_bytes;
mod transmute_ptr_to_ptr;
mod transmute_ptr_to_ref;
"transmutes from a null pointer to a reference, which is undefined behavior"
}
+declare_clippy_lint! {
+ /// ### What it does
+ /// Checks for null function pointer creation through transmute.
+ ///
+ /// ### Why is this bad?
+ /// Creating a null function pointer is undefined behavior.
+ ///
+ /// More info: https://doc.rust-lang.org/nomicon/ffi.html#the-nullable-pointer-optimization
+ ///
+ /// ### Known problems
+ /// Not all cases can be detected at the moment of this writing.
+ /// For example, variables which hold a null pointer and are then fed to a `transmute`
+ /// call, aren't detectable yet.
+ ///
+ /// ### Example
+ /// ```rust
+ /// let null_fn: fn() = unsafe { std::mem::transmute( std::ptr::null::<()>() ) };
+ /// ```
+ /// Use instead:
+ /// ```rust
+ /// let null_fn: Option<fn()> = None;
+ /// ```
+ #[clippy::version = "1.67.0"]
+ pub TRANSMUTE_NULL_TO_FN,
+ correctness,
+ "transmute results in a null function pointer, which is undefined behavior"
+}
+
pub struct Transmute {
msrv: Msrv,
}
TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
TRANSMUTE_UNDEFINED_REPR,
TRANSMUTING_NULL,
+ TRANSMUTE_NULL_TO_FN,
]);
impl Transmute {
#[must_use]
let linted = wrong_transmute::check(cx, e, from_ty, to_ty)
| crosspointer_transmute::check(cx, e, from_ty, to_ty)
| transmuting_null::check(cx, e, arg, to_ty)
+ | transmute_null_to_fn::check(cx, e, arg, to_ty)
| transmute_ptr_to_ref::check(cx, e, from_ty, to_ty, arg, path, &self.msrv)
| transmute_int_to_char::check(cx, e, from_ty, to_ty, arg, const_context)
| transmute_ref_to_ref::check(cx, e, from_ty, to_ty, arg, const_context)