Create rule S7200: Avoid resizing a vector to zero using vec.resize(0, value) (#4677)

This commit is contained in:
github-actions[bot] 2025-03-18 13:40:42 +01:00 committed by GitHub
parent 734c90357c
commit f7e3f45cf3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 57 additions and 0 deletions

View File

@ -45,6 +45,7 @@ When web pages have massively long names like "Java™ Platform, Standard Editio
* AWS blog - https://aws.amazon.com/blogs
* Azure Documentation - https://learn.microsoft.com/en-us/azure/?product=popular
* 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} Core Guidelines - https://github.com/isocpp/CppCoreGuidelines/blob/e49158a/CppCoreGuidelines.md
* CVE - https://cve.mitre.org

View File

@ -0,0 +1,2 @@
{
}

View 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"
}
}

View 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