31 lines
913 B
Plaintext
31 lines
913 B
Plaintext
== 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
|