Create rule S4962: "std::ptr::null" should be used to denote the null pointer (#4697)
* Add rust to rule S4962 * Remove tag --------- 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
e34bf94e7d
commit
0b6c98a1f0
@ -1,2 +1,3 @@
|
||||
{
|
||||
|
||||
}
|
||||
|
34
rules/S4962/rust/metadata.json
Normal file
34
rules/S4962/rust/metadata.json
Normal file
@ -0,0 +1,34 @@
|
||||
{
|
||||
"title": "\"std::ptr::null\" should be used to denote the null pointer",
|
||||
"type": "CODE_SMELL",
|
||||
"code": {
|
||||
"impacts": {
|
||||
"MAINTAINABILITY": "HIGH"
|
||||
},
|
||||
"attribute": "CONVENTIONAL"
|
||||
},
|
||||
"status": "ready",
|
||||
"remediation": {
|
||||
"func": "Constant\/Issue",
|
||||
"constantCost": "1min"
|
||||
},
|
||||
"tags": [
|
||||
"clippy"
|
||||
],
|
||||
"extra": {
|
||||
"replacementRules": [
|
||||
|
||||
],
|
||||
"legacyKeys": [
|
||||
|
||||
]
|
||||
},
|
||||
"defaultSeverity": "Critical",
|
||||
"ruleSpecification": "RSPEC-4962",
|
||||
"sqKey": "S4962",
|
||||
"scope": "All",
|
||||
"defaultQualityProfiles": [
|
||||
"Sonar way"
|
||||
],
|
||||
"quickfix": "unknown"
|
||||
}
|
30
rules/S4962/rust/rule.adoc
Normal file
30
rules/S4962/rust/rule.adoc
Normal file
@ -0,0 +1,30 @@
|
||||
== Why is this an issue?
|
||||
|
||||
Using ``++0 as *const T++`` or ``++0 as *mut T++`` to represent a null pointer is error-prone and less readable. It can lead to confusion and potential bugs, as it is not immediately clear that `0` is intended to represent a null pointer. Additionally, using ``++0++`` for null pointers is not idiomatic Rust, and it can make the code harder to understand and maintain.
|
||||
|
||||
== How to fix it
|
||||
|
||||
Use ``++std::ptr::null++`` or ``++std::ptr::null_mut++`` to represent null pointers. These functions are explicitly designed for this purpose and make the intent of the code clear.
|
||||
|
||||
=== Code examples
|
||||
|
||||
==== Noncompliant code example
|
||||
|
||||
[source,rust,diff-id=1,diff-type=noncompliant]
|
||||
----
|
||||
let ptr = 0 as *const i32;
|
||||
let mut_ptr = 0 as *mut i32;
|
||||
----
|
||||
|
||||
==== Compliant solution
|
||||
|
||||
[source,rust,diff-id=1,diff-type=compliant]
|
||||
----
|
||||
let ptr = std::ptr::null::<i32>();
|
||||
let mut_ptr = std::ptr::null_mut::<i32>();
|
||||
----
|
||||
|
||||
== Resources
|
||||
=== Documentation
|
||||
|
||||
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#zero_ptr
|
Loading…
x
Reference in New Issue
Block a user