rspec/rules/S7429/rust/rule.adoc

25 lines
691 B
Plaintext
Raw Permalink Normal View History

== Why is this an issue?
Creating a null function pointer using `transmute` results in undefined behavior, as null function pointers are not valid in Rust. Instead, use `Option<fn()>` to safely represent a nullable function pointer.
=== Code examples
==== Noncompliant code example
[source,rust,diff-id=1,diff-type=noncompliant]
----
let null_fn: fn() = unsafe { std::mem::transmute(std::ptr::null::<()>()) }; // Noncompliant
----
==== Compliant solution
[source,rust,diff-id=1,diff-type=compliant]
----
let null_fn: Option<fn()> = None; // Compliant
----
== Resources
=== Documentation
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#transmute_null_to_fn