From f7e3f45cf3da149dbb772bb9fc91c626f7c4499d Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 18 Mar 2025 13:40:42 +0100 Subject: [PATCH] Create rule S7200: Avoid resizing a vector to zero using `vec.resize(0, value)` (#4677) --- docs/link_formatting.adoc | 1 + rules/S7200/metadata.json | 2 ++ rules/S7200/rust/metadata.json | 24 ++++++++++++++++++++++++ rules/S7200/rust/rule.adoc | 30 ++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 rules/S7200/metadata.json create mode 100644 rules/S7200/rust/metadata.json create mode 100644 rules/S7200/rust/rule.adoc diff --git a/docs/link_formatting.adoc b/docs/link_formatting.adoc index faa220e686..9d5e305f1c 100644 --- a/docs/link_formatting.adoc +++ b/docs/link_formatting.adoc @@ -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 diff --git a/rules/S7200/metadata.json b/rules/S7200/metadata.json new file mode 100644 index 0000000000..2c63c08510 --- /dev/null +++ b/rules/S7200/metadata.json @@ -0,0 +1,2 @@ +{ +} diff --git a/rules/S7200/rust/metadata.json b/rules/S7200/rust/metadata.json new file mode 100644 index 0000000000..e81f240918 --- /dev/null +++ b/rules/S7200/rust/metadata.json @@ -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" + } +} diff --git a/rules/S7200/rust/rule.adoc b/rules/S7200/rust/rule.adoc new file mode 100644 index 0000000000..66cb898442 --- /dev/null +++ b/rules/S7200/rust/rule.adoc @@ -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