Create rule S7414: Avoid transmutes that can never be correct (#4753)

* Create rule S7414

* 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:55 +00:00 committed by GitHub
parent 87a178f05b
commit ea0dd90530
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": "Avoid transmutes that can never be correct",
"type": "BUG",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"clippy"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-7414",
"sqKey": "S7414",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "unknown",
"code": {
"impacts": {
"RELIABILITY": "HIGH"
},
"attribute": "LOGICAL"
}
}

View File

@ -0,0 +1,24 @@
== Why is this an issue?
Using ``++core::intrinsics::transmute++`` in a way that is not valid on any architecture leads to undefined behavior. This can result in unpredictable program behavior or crashes.
=== Code examples
==== Noncompliant code example
[source,rust,diff-id=1,diff-type=noncompliant]
----
let ptr: *const T = core::intrinsics::transmute('x'); // Noncompliant: Invalid transmute operation.
----
==== Compliant solution
[source,rust,diff-id=1,diff-type=compliant]
----
let value: T = 'x' as T; // Compliant: Use a safe conversion method.
----
== Resources
=== Documentation
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#wrong_transmute