Lint only the first statment/expression after alloc

Instead of searching for all the successive expressions after a vector
allocation, check only the first expression.

This is done to minimize the amount of false positives of the lint.
This commit is contained in:
Guillem Nieto 2018-11-18 23:45:53 +01:00 committed by Guillem Nieto
parent 39b02fdcd2
commit 5fa04bc3cd
3 changed files with 49 additions and 19 deletions

View file

@ -34,13 +34,13 @@ fn extend_vector() {
fn mixed_extend_resize_vector() {
// Mismatching len
let mut mismatching_len = Vec::with_capacity(30);
mismatching_len.extend(repeat(0).take(40));
// Slow initialization
let mut resized_vec = Vec::with_capacity(30);
let mut extend_vec = Vec::with_capacity(30);
resized_vec.resize(30, 0);
mismatching_len.extend(repeat(0).take(40));
let mut extend_vec = Vec::with_capacity(30);
extend_vec.extend(repeat(0).take(30));
}
@ -70,3 +70,14 @@ fn unsafe_vector() {
unsafe_vec.set_len(200);
}
}
fn do_stuff(vec: &mut Vec<u8>) {
}
fn extend_vector_with_manipulations_between() {
let len = 300;
let mut vec1:Vec<u8> = Vec::with_capacity(len);
do_stuff(&mut vec1);
vec1.extend(repeat(0).take(len));
}

View file

@ -17,20 +17,18 @@ error: slow zero-filling initialization
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: slow zero-filling initialization
--> $DIR/slow_vector_initialization.rs:42:5
--> $DIR/slow_vector_initialization.rs:41:5
|
39 | let mut resized_vec = Vec::with_capacity(30);
40 | let mut resized_vec = Vec::with_capacity(30);
| ---------------------- help: consider replace allocation with: `vec![0; 30]`
...
42 | resized_vec.resize(30, 0);
41 | resized_vec.resize(30, 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^
error: slow zero-filling initialization
--> $DIR/slow_vector_initialization.rs:44:5
|
40 | let mut extend_vec = Vec::with_capacity(30);
43 | let mut extend_vec = Vec::with_capacity(30);
| ---------------------- help: consider replace allocation with: `vec![0; 30]`
...
44 | extend_vec.extend(repeat(0).take(30));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^