Create rule S7426 C-like enums should not have unportable variants (#4767)
* Create rule S7426 * Update rule.adoc * Update metadata.json * Update metadata.json --------- Co-authored-by: sallaigy <sallaigy@users.noreply.github.com> Co-authored-by: Gyula Sallai <gyula.sallai@sonarsource.com>
This commit is contained in:
parent
c83072239e
commit
8e35213fbb
2
rules/S7426/metadata.json
Normal file
2
rules/S7426/metadata.json
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
{
|
||||||
|
}
|
24
rules/S7426/rust/metadata.json
Normal file
24
rules/S7426/rust/metadata.json
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"title": "C-like enums should not have unportable variants",
|
||||||
|
"type": "BUG",
|
||||||
|
"status": "ready",
|
||||||
|
"remediation": {
|
||||||
|
"func": "Constant\/Issue",
|
||||||
|
"constantCost": "5min"
|
||||||
|
},
|
||||||
|
"tags": [
|
||||||
|
"clippy"
|
||||||
|
],
|
||||||
|
"defaultSeverity": "Critical",
|
||||||
|
"ruleSpecification": "RSPEC-7426",
|
||||||
|
"sqKey": "S7426",
|
||||||
|
"scope": "All",
|
||||||
|
"defaultQualityProfiles": ["Sonar way"],
|
||||||
|
"quickfix": "unknown",
|
||||||
|
"code": {
|
||||||
|
"impacts": {
|
||||||
|
"RELIABILITY": "HIGH"
|
||||||
|
},
|
||||||
|
"attribute": "LOGICAL"
|
||||||
|
}
|
||||||
|
}
|
32
rules/S7426/rust/rule.adoc
Normal file
32
rules/S7426/rust/rule.adoc
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
|
||||||
|
== Why is this an issue?
|
||||||
|
Using `repr(isize/usize)` for C-like enumerations with values that don't fit into an `i32` can lead to truncated variant values on 32-bit architectures, potentially causing unexpected behavior.
|
||||||
|
|
||||||
|
|
||||||
|
=== Code examples
|
||||||
|
|
||||||
|
==== Noncompliant code example
|
||||||
|
[source,rust,diff-id=1,diff-type=noncompliant]
|
||||||
|
----
|
||||||
|
#[repr(usize)]
|
||||||
|
enum NonPortable {
|
||||||
|
X = 0x1_0000_0000, // Noncompliant
|
||||||
|
Y = 0,
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|
||||||
|
==== Compliant solution
|
||||||
|
|
||||||
|
[source,rust,diff-id=1,diff-type=compliant]
|
||||||
|
----
|
||||||
|
#[repr(u64)]
|
||||||
|
enum Portable {
|
||||||
|
X = 0x1_0000_0000, // Compliant
|
||||||
|
Y = 0,
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|
||||||
|
== Resources
|
||||||
|
=== Documentation
|
||||||
|
|
||||||
|
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#enum_clike_unportable_variant
|
Loading…
x
Reference in New Issue
Block a user