From 90a9b01d260c1f28de1c720c224b69c511a4c7d0 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 19 Mar 2025 13:37:38 +0000 Subject: [PATCH] Create rule S7089: Inline vector literals should be preferred to chains of insertions (#4741) * Add rust to rule S7089 * Update RSPEC --------- Co-authored-by: yassin-kammoun-sonarsource Co-authored-by: yassin-kammoun-sonarsource --- rules/S7089/dart/metadata.json | 21 --------------------- rules/S7089/metadata.json | 21 +++++++++++++++++++++ rules/S7089/rust/metadata.json | 6 ++++++ rules/S7089/rust/rule.adoc | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 21 deletions(-) create mode 100644 rules/S7089/rust/metadata.json create mode 100644 rules/S7089/rust/rule.adoc diff --git a/rules/S7089/dart/metadata.json b/rules/S7089/dart/metadata.json index 75bdfff969..2c63c08510 100644 --- a/rules/S7089/dart/metadata.json +++ b/rules/S7089/dart/metadata.json @@ -1,23 +1,2 @@ { - "title": "Inline list literals should be preferred to chains of insertions", - "type": "CODE_SMELL", - "status": "ready", - "remediation": { - "func": "Constant\/Issue", - "constantCost": "5min" - }, - "tags": [ - ], - "defaultSeverity": "Major", - "ruleSpecification": "RSPEC-7089", - "sqKey": "S7089", - "scope": "All", - "defaultQualityProfiles": ["Sonar way"], - "quickfix": "unknown", - "code": { - "impacts": { - "MAINTAINABILITY": "MEDIUM" - }, - "attribute": "EFFICIENT" - } } diff --git a/rules/S7089/metadata.json b/rules/S7089/metadata.json index 2c63c08510..75bdfff969 100644 --- a/rules/S7089/metadata.json +++ b/rules/S7089/metadata.json @@ -1,2 +1,23 @@ { + "title": "Inline list literals should be preferred to chains of insertions", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + ], + "defaultSeverity": "Major", + "ruleSpecification": "RSPEC-7089", + "sqKey": "S7089", + "scope": "All", + "defaultQualityProfiles": ["Sonar way"], + "quickfix": "unknown", + "code": { + "impacts": { + "MAINTAINABILITY": "MEDIUM" + }, + "attribute": "EFFICIENT" + } } diff --git a/rules/S7089/rust/metadata.json b/rules/S7089/rust/metadata.json new file mode 100644 index 0000000000..7f1a107e92 --- /dev/null +++ b/rules/S7089/rust/metadata.json @@ -0,0 +1,6 @@ +{ + "title": "Inline vector literals should be preferred to chains of insertions", + "tags": [ + "clippy" + ] +} diff --git a/rules/S7089/rust/rule.adoc b/rules/S7089/rust/rule.adoc new file mode 100644 index 0000000000..46d317ff37 --- /dev/null +++ b/rules/S7089/rust/rule.adoc @@ -0,0 +1,34 @@ +== Why is this an issue? + +Initializing a vector with a chain of ``++push++`` invocations is less efficient than making the initialization with a single vector literal. Each invocation has a cost in itself. + +On top of the performance argument, the vector literal is more concise and easier to read. + +== How to fix it + +Introduce a single vector literal that includes all the elements that are added to the vector as arguments of the ``++push++`` invocations. + +=== Code examples + +==== Noncompliant code example + +[source,rust,diff-id=1,diff-type=noncompliant] +---- +let mut v = Vec::new(); +v.push(0); +v.push(1); +v.push(2); +---- + +==== Compliant solution + +[source,rust,diff-id=1,diff-type=compliant] +---- +let v = vec![0, 1, 2]; +---- + +== Resources + +=== Documentation + +* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#vec_init_then_push