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:
parent
ec02230f32
commit
87a178f05b
2
rules/S7412/metadata.json
Normal file
2
rules/S7412/metadata.json
Normal file
@ -0,0 +1,2 @@
|
||||
{
|
||||
}
|
24
rules/S7412/rust/metadata.json
Normal file
24
rules/S7412/rust/metadata.json
Normal 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"
|
||||
}
|
||||
}
|
24
rules/S7412/rust/rule.adoc
Normal file
24
rules/S7412/rust/rule.adoc
Normal 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
|
Loading…
x
Reference in New Issue
Block a user