Create rule S7200: Avoid resizing a vector to zero using vec.resize(0, value)
(#4677)
This commit is contained in:
parent
734c90357c
commit
f7e3f45cf3
@ -45,6 +45,7 @@ When web pages have massively long names like "Java™ Platform, Standard Editio
|
|||||||
* AWS blog - https://aws.amazon.com/blogs
|
* AWS blog - https://aws.amazon.com/blogs
|
||||||
* Azure Documentation - https://learn.microsoft.com/en-us/azure/?product=popular
|
* Azure Documentation - https://learn.microsoft.com/en-us/azure/?product=popular
|
||||||
* CERT - https://wiki.sei.cmu.edu/confluence/display/seccode
|
* CERT - https://wiki.sei.cmu.edu/confluence/display/seccode
|
||||||
|
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html
|
||||||
* {cpp} reference - https://en.cppreference.com/w/
|
* {cpp} reference - https://en.cppreference.com/w/
|
||||||
* {cpp} Core Guidelines - https://github.com/isocpp/CppCoreGuidelines/blob/e49158a/CppCoreGuidelines.md
|
* {cpp} Core Guidelines - https://github.com/isocpp/CppCoreGuidelines/blob/e49158a/CppCoreGuidelines.md
|
||||||
* CVE - https://cve.mitre.org
|
* CVE - https://cve.mitre.org
|
||||||
|
2
rules/S7200/metadata.json
Normal file
2
rules/S7200/metadata.json
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
{
|
||||||
|
}
|
24
rules/S7200/rust/metadata.json
Normal file
24
rules/S7200/rust/metadata.json
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"title": "Avoid resizing a vector to zero using `vec.resize(0, value)`",
|
||||||
|
"type": "BUG",
|
||||||
|
"status": "ready",
|
||||||
|
"remediation": {
|
||||||
|
"func": "Constant\/Issue",
|
||||||
|
"constantCost": "5min"
|
||||||
|
},
|
||||||
|
"tags": [
|
||||||
|
"clippy"
|
||||||
|
],
|
||||||
|
"defaultSeverity": "Major",
|
||||||
|
"ruleSpecification": "RSPEC-7200",
|
||||||
|
"sqKey": "S7200",
|
||||||
|
"scope": "All",
|
||||||
|
"defaultQualityProfiles": ["Sonar way"],
|
||||||
|
"quickfix": "unknown",
|
||||||
|
"code": {
|
||||||
|
"impacts": {
|
||||||
|
"MAINTAINABILITY": "LOW"
|
||||||
|
},
|
||||||
|
"attribute": "CLEAR"
|
||||||
|
}
|
||||||
|
}
|
30
rules/S7200/rust/rule.adoc
Normal file
30
rules/S7200/rust/rule.adoc
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
== Why is this an issue?
|
||||||
|
|
||||||
|
Resizing a vector to zero using `vec.resize(0, value)` is misleading. It's either unreadable if the intent was simply to clear the vector, making the code harder to understand, or suspicious and unintentional if a resize was actually expected, but the arguments were accidentally swapped.
|
||||||
|
|
||||||
|
== How to fix it
|
||||||
|
|
||||||
|
Replace `vec.resize(0, value)` with `vec.clear()`, or swap the `vec.resize` arguments.
|
||||||
|
|
||||||
|
=== Code examples
|
||||||
|
|
||||||
|
==== Noncompliant code example
|
||||||
|
|
||||||
|
[source,rust,diff-id=1,diff-type=noncompliant]
|
||||||
|
----
|
||||||
|
let mut vec = vec![1, 2, 3, 4, 5];
|
||||||
|
vec.resize(0, 5); // Noncompliant: Resizing the vector to 0.
|
||||||
|
----
|
||||||
|
|
||||||
|
==== Compliant solution
|
||||||
|
|
||||||
|
[source,rust,diff-id=1,diff-type=compliant]
|
||||||
|
----
|
||||||
|
let mut vec = vec![1, 2, 3, 4, 5];
|
||||||
|
vec.clear(); // Compliant: Clear the vector.
|
||||||
|
----
|
||||||
|
|
||||||
|
== Resources
|
||||||
|
=== Documentation
|
||||||
|
|
||||||
|
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#vec_resize_to_zero
|
Loading…
x
Reference in New Issue
Block a user