rspec/rules/S7412/rust/rule.adoc
github-actions[bot] 87a178f05b
Create rule S7412: Pointer arithmetic should not be performed on zero-sized types (#4751)
* Create rule S7412

* 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:07:48 +00:00

25 lines
707 B
Plaintext

== Why is this an issue?
Performing operations like `offset` or `wrapping_add`/`wrapping_sub` on raw pointers to zero-sized types (ZSTs) results in no-op operations. This is likely unintended and could make the code misleading, potentially hiding bugs.
=== Code examples
==== Noncompliant code example
[source,rust,diff-id=1,diff-type=noncompliant]
----
unsafe { (&() as *const ()).offset(1); } // Noncompliant: No-op on zero-sized type pointer.
----
==== Compliant solution
[source,rust,diff-id=1,diff-type=compliant]
----
// Removing the no-op operation as it has no effect.
----
== Resources
=== Documentation
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#zst_offset