Create rule S7425: MaybeUninit::uninit().assume_init()
should not be used (#4766)
* Create rule S7425 * 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
ea0dd90530
commit
e94e4d8143
2
rules/S7425/metadata.json
Normal file
2
rules/S7425/metadata.json
Normal file
@ -0,0 +1,2 @@
|
||||
{
|
||||
}
|
24
rules/S7425/rust/metadata.json
Normal file
24
rules/S7425/rust/metadata.json
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"title": "`MaybeUninit::uninit().assume_init()` should not be used",
|
||||
"type": "BUG",
|
||||
"status": "ready",
|
||||
"remediation": {
|
||||
"func": "Constant\/Issue",
|
||||
"constantCost": "5min"
|
||||
},
|
||||
"tags": [
|
||||
"clippy"
|
||||
],
|
||||
"defaultSeverity": "Major",
|
||||
"ruleSpecification": "RSPEC-7425",
|
||||
"sqKey": "S7425",
|
||||
"scope": "All",
|
||||
"defaultQualityProfiles": ["Sonar way"],
|
||||
"quickfix": "unknown",
|
||||
"code": {
|
||||
"impacts": {
|
||||
"RELIABILITY": "MEDIUM"
|
||||
},
|
||||
"attribute": "LOGICAL"
|
||||
}
|
||||
}
|
28
rules/S7425/rust/rule.adoc
Normal file
28
rules/S7425/rust/rule.adoc
Normal file
@ -0,0 +1,28 @@
|
||||
== Why is this an issue?
|
||||
|
||||
Using ``++MaybeUninit::uninit().assume_init()++`` results in undefined behavior for most types, since it bypasses initialization and assumes the data is valid. This can lead to unpredictable results and hard-to-diagnose bugs.
|
||||
|
||||
=== Code examples
|
||||
|
||||
==== Noncompliant code example
|
||||
|
||||
[source,rust,diff-id=1,diff-type=noncompliant]
|
||||
----
|
||||
use std::mem::MaybeUninit;
|
||||
|
||||
let _: usize = unsafe { MaybeUninit::uninit().assume_init() }; // Noncompliant: Unsafe code resulting in undefined behavior.
|
||||
----
|
||||
|
||||
==== Compliant solution
|
||||
|
||||
[source,rust,diff-id=1,diff-type=compliant]
|
||||
----
|
||||
use std::mem::MaybeUninit;
|
||||
|
||||
let _: [MaybeUninit<bool>; 5] = unsafe { MaybeUninit::uninit().assume_init() }; // Compliant: Allowed for tuples/arrays of MaybeUninit.
|
||||
----
|
||||
|
||||
== Resources
|
||||
=== Documentation
|
||||
|
||||
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#uninit_assumed_init
|
Loading…
x
Reference in New Issue
Block a user