-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve
slow_vector_initialization
suggestion (#13912)
close #13781 The `slow_vector_initialization` lint currently only suggests using the `vec!` macro with size, but it does not suggest removing the `resize` method call. changelog: [`slow_vector_initialization`]: improve `slow_vector_initialization` suggestion
- Loading branch information
Showing
3 changed files
with
104 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,122 @@ | ||
error: slow zero-filling initialization | ||
--> tests/ui/slow_vector_initialization.rs:14:5 | ||
--> tests/ui/slow_vector_initialization.rs:13:20 | ||
| | ||
LL | let mut vec1 = Vec::with_capacity(len); | ||
| ----------------------- help: consider replacing this with: `vec![0; len]` | ||
LL | vec1.extend(repeat(0).take(len)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
LL | let mut vec1 = Vec::with_capacity(len); | ||
| ____________________^ | ||
... | | ||
LL | | vec1.extend(repeat(0).take(len)); | ||
| |____________________________________^ help: consider replacing this with: `vec![0; len]` | ||
| | ||
= note: `-D clippy::slow-vector-initialization` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::slow_vector_initialization)]` | ||
|
||
error: slow zero-filling initialization | ||
--> tests/ui/slow_vector_initialization.rs:20:5 | ||
--> tests/ui/slow_vector_initialization.rs:19:20 | ||
| | ||
LL | let mut vec2 = Vec::with_capacity(len - 10); | ||
| ---------------------------- help: consider replacing this with: `vec![0; len - 10]` | ||
LL | vec2.extend(repeat(0).take(len - 10)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
LL | let mut vec2 = Vec::with_capacity(len - 10); | ||
| ____________________^ | ||
LL | | | ||
LL | | vec2.extend(repeat(0).take(len - 10)); | ||
| |_________________________________________^ help: consider replacing this with: `vec![0; len - 10]` | ||
|
||
error: slow zero-filling initialization | ||
--> tests/ui/slow_vector_initialization.rs:28:5 | ||
--> tests/ui/slow_vector_initialization.rs:27:20 | ||
| | ||
LL | let mut vec4 = Vec::with_capacity(len); | ||
| ----------------------- help: consider replacing this with: `vec![0; len]` | ||
LL | vec4.extend(repeat(0).take(vec4.capacity())); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
LL | let mut vec4 = Vec::with_capacity(len); | ||
| ____________________^ | ||
LL | | | ||
LL | | vec4.extend(repeat(0).take(vec4.capacity())); | ||
| |________________________________________________^ help: consider replacing this with: `vec![0; len]` | ||
|
||
error: slow zero-filling initialization | ||
--> tests/ui/slow_vector_initialization.rs:39:5 | ||
--> tests/ui/slow_vector_initialization.rs:38:27 | ||
| | ||
LL | let mut resized_vec = Vec::with_capacity(30); | ||
| ---------------------- help: consider replacing this with: `vec![0; 30]` | ||
LL | resized_vec.resize(30, 0); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
LL | let mut resized_vec = Vec::with_capacity(30); | ||
| ___________________________^ | ||
LL | | | ||
LL | | resized_vec.resize(30, 0); | ||
| |_____________________________^ help: consider replacing this with: `vec![0; 30]` | ||
|
||
error: slow zero-filling initialization | ||
--> tests/ui/slow_vector_initialization.rs:43:5 | ||
--> tests/ui/slow_vector_initialization.rs:42:26 | ||
| | ||
LL | let mut extend_vec = Vec::with_capacity(30); | ||
| ---------------------- help: consider replacing this with: `vec![0; 30]` | ||
LL | extend_vec.extend(repeat(0).take(30)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
LL | let mut extend_vec = Vec::with_capacity(30); | ||
| __________________________^ | ||
LL | | | ||
LL | | extend_vec.extend(repeat(0).take(30)); | ||
| |_________________________________________^ help: consider replacing this with: `vec![0; 30]` | ||
|
||
error: slow zero-filling initialization | ||
--> tests/ui/slow_vector_initialization.rs:51:5 | ||
--> tests/ui/slow_vector_initialization.rs:50:20 | ||
| | ||
LL | let mut vec1 = Vec::with_capacity(len); | ||
| ----------------------- help: consider replacing this with: `vec![0; len]` | ||
LL | vec1.resize(len, 0); | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
LL | let mut vec1 = Vec::with_capacity(len); | ||
| ____________________^ | ||
LL | | | ||
LL | | vec1.resize(len, 0); | ||
| |_______________________^ help: consider replacing this with: `vec![0; len]` | ||
|
||
error: slow zero-filling initialization | ||
--> tests/ui/slow_vector_initialization.rs:60:5 | ||
--> tests/ui/slow_vector_initialization.rs:59:20 | ||
| | ||
LL | let mut vec3 = Vec::with_capacity(len - 10); | ||
| ---------------------------- help: consider replacing this with: `vec![0; len - 10]` | ||
LL | vec3.resize(len - 10, 0); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
LL | let mut vec3 = Vec::with_capacity(len - 10); | ||
| ____________________^ | ||
LL | | | ||
LL | | vec3.resize(len - 10, 0); | ||
| |____________________________^ help: consider replacing this with: `vec![0; len - 10]` | ||
|
||
error: slow zero-filling initialization | ||
--> tests/ui/slow_vector_initialization.rs:64:5 | ||
--> tests/ui/slow_vector_initialization.rs:63:20 | ||
| | ||
LL | let mut vec4 = Vec::with_capacity(len); | ||
| ----------------------- help: consider replacing this with: `vec![0; len]` | ||
LL | vec4.resize(vec4.capacity(), 0); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
LL | let mut vec4 = Vec::with_capacity(len); | ||
| ____________________^ | ||
LL | | | ||
LL | | vec4.resize(vec4.capacity(), 0); | ||
| |___________________________________^ help: consider replacing this with: `vec![0; len]` | ||
|
||
error: slow zero-filling initialization | ||
--> tests/ui/slow_vector_initialization.rs:69:5 | ||
--> tests/ui/slow_vector_initialization.rs:68:12 | ||
| | ||
LL | vec1 = Vec::with_capacity(10); | ||
| ---------------------- help: consider replacing this with: `vec![0; 10]` | ||
LL | vec1.resize(10, 0); | ||
| ^^^^^^^^^^^^^^^^^^ | ||
LL | vec1 = Vec::with_capacity(10); | ||
| ____________^ | ||
LL | | | ||
LL | | vec1.resize(10, 0); | ||
| |______________________^ help: consider replacing this with: `vec![0; 10]` | ||
|
||
error: slow zero-filling initialization | ||
--> tests/ui/slow_vector_initialization.rs:77:5 | ||
--> tests/ui/slow_vector_initialization.rs:76:20 | ||
| | ||
LL | let mut vec1 = Vec::new(); | ||
| ---------- help: consider replacing this with: `vec![0; len]` | ||
LL | vec1.resize(len, 0); | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
LL | let mut vec1 = Vec::new(); | ||
| ____________________^ | ||
LL | | | ||
LL | | vec1.resize(len, 0); | ||
| |_______________________^ help: consider replacing this with: `vec![0; len]` | ||
|
||
error: slow zero-filling initialization | ||
--> tests/ui/slow_vector_initialization.rs:82:5 | ||
--> tests/ui/slow_vector_initialization.rs:81:20 | ||
| | ||
LL | let mut vec3 = Vec::new(); | ||
| ---------- help: consider replacing this with: `vec![0; len - 10]` | ||
LL | vec3.resize(len - 10, 0); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
LL | let mut vec3 = Vec::new(); | ||
| ____________________^ | ||
LL | | | ||
LL | | vec3.resize(len - 10, 0); | ||
| |____________________________^ help: consider replacing this with: `vec![0; len - 10]` | ||
|
||
error: slow zero-filling initialization | ||
--> tests/ui/slow_vector_initialization.rs:87:5 | ||
--> tests/ui/slow_vector_initialization.rs:86:12 | ||
| | ||
LL | vec1 = Vec::new(); | ||
| ---------- help: consider replacing this with: `vec![0; 10]` | ||
LL | vec1.resize(10, 0); | ||
| ^^^^^^^^^^^^^^^^^^ | ||
LL | vec1 = Vec::new(); | ||
| ____________^ | ||
LL | | | ||
LL | | vec1.resize(10, 0); | ||
| |______________________^ help: consider replacing this with: `vec![0; 10]` | ||
|
||
error: slow zero-filling initialization | ||
--> tests/ui/slow_vector_initialization.rs:91:5 | ||
--> tests/ui/slow_vector_initialization.rs:90:12 | ||
| | ||
LL | vec1 = vec![]; | ||
| ------ help: consider replacing this with: `vec![0; 10]` | ||
LL | vec1.resize(10, 0); | ||
| ^^^^^^^^^^^^^^^^^^ | ||
LL | vec1 = vec![]; | ||
| ____________^ | ||
LL | | | ||
LL | | vec1.resize(10, 0); | ||
| |______________________^ help: consider replacing this with: `vec![0; 10]` | ||
|
||
error: aborting due to 13 previous errors | ||
|