rspec/rules/S7429/rust/rule.adoc
github-actions[bot] 31e8111116
Create rule S7429: Null function pointers should not be created through transmute (#4770)
* Create rule S7429

* Update RSPEC

---------

Co-authored-by: yassin-kammoun-sonarsource <yassin-kammoun-sonarsource@users.noreply.github.com>
Co-authored-by: yassin-kammoun-sonarsource <yassin.kammoun@sonarsource.com>
2025-03-19 13:06:59 +00:00

25 lines
691 B
Plaintext

== 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