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>
This commit is contained in:
github-actions[bot] 2025-03-19 13:07:48 +00:00 committed by GitHub
parent ec02230f32
commit 87a178f05b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 50 additions and 0 deletions

View File

@ -0,0 +1,2 @@
{
}

View File

@ -0,0 +1,24 @@
{
"title": "Pointer arithmetic should not be performed on zero-sized types",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"clippy"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-7412",
"sqKey": "S7412",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "unknown",
"code": {
"impacts": {
"MAINTAINABILITY": "MEDIUM"
},
"attribute": "LOGICAL"
}
}

View File

@ -0,0 +1,24 @@
== 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