![github-actions[bot]](/assets/img/avatar_default.png)
* Add rust to rule S7089 * Update RSPEC --------- Co-authored-by: yassin-kammoun-sonarsource <yassin-kammoun-sonarsource@users.noreply.github.com> Co-authored-by: yassin-kammoun-sonarsource <yassin.kammoun@sonarsource.com>
35 lines
854 B
Plaintext
35 lines
854 B
Plaintext
== 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
|