2023-08-24 14:32:12 -05:00
|
|
|
//@no-rustfix
|
2023-09-12 11:13:53 -05:00
|
|
|
use std::iter::repeat;
|
2018-10-18 18:15:48 -05:00
|
|
|
fn main() {
|
|
|
|
resize_vector();
|
|
|
|
extend_vector();
|
|
|
|
mixed_extend_resize_vector();
|
2023-07-31 16:53:53 -05:00
|
|
|
from_empty_vec();
|
2018-10-18 18:15:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn extend_vector() {
|
|
|
|
// Extend with constant expression
|
|
|
|
let len = 300;
|
|
|
|
let mut vec1 = Vec::with_capacity(len);
|
|
|
|
vec1.extend(repeat(0).take(len));
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: slow zero-filling initialization
|
|
|
|
//~| NOTE: `-D clippy::slow-vector-initialization` implied by `-D warnings`
|
2018-10-18 18:15:48 -05:00
|
|
|
|
|
|
|
// Extend with len expression
|
|
|
|
let mut vec2 = Vec::with_capacity(len - 10);
|
|
|
|
vec2.extend(repeat(0).take(len - 10));
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: slow zero-filling initialization
|
2018-10-18 18:15:48 -05:00
|
|
|
|
|
|
|
// Extend with mismatching expression should not be warned
|
|
|
|
let mut vec3 = Vec::with_capacity(24322);
|
|
|
|
vec3.extend(repeat(0).take(2));
|
2022-06-30 03:50:09 -05:00
|
|
|
|
|
|
|
let mut vec4 = Vec::with_capacity(len);
|
|
|
|
vec4.extend(repeat(0).take(vec4.capacity()));
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: slow zero-filling initialization
|
2018-10-18 18:15:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn mixed_extend_resize_vector() {
|
|
|
|
// Mismatching len
|
|
|
|
let mut mismatching_len = Vec::with_capacity(30);
|
2018-11-18 16:45:53 -06:00
|
|
|
mismatching_len.extend(repeat(0).take(40));
|
2018-10-18 18:15:48 -05:00
|
|
|
|
|
|
|
// Slow initialization
|
|
|
|
let mut resized_vec = Vec::with_capacity(30);
|
|
|
|
resized_vec.resize(30, 0);
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: slow zero-filling initialization
|
2018-11-18 16:45:53 -06:00
|
|
|
|
|
|
|
let mut extend_vec = Vec::with_capacity(30);
|
2018-10-18 18:15:48 -05:00
|
|
|
extend_vec.extend(repeat(0).take(30));
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: slow zero-filling initialization
|
2018-10-18 18:15:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn resize_vector() {
|
|
|
|
// Resize with constant expression
|
|
|
|
let len = 300;
|
|
|
|
let mut vec1 = Vec::with_capacity(len);
|
|
|
|
vec1.resize(len, 0);
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: slow zero-filling initialization
|
2018-10-18 18:15:48 -05:00
|
|
|
|
|
|
|
// Resize mismatch len
|
|
|
|
let mut vec2 = Vec::with_capacity(200);
|
|
|
|
vec2.resize(10, 0);
|
|
|
|
|
|
|
|
// Resize with len expression
|
|
|
|
let mut vec3 = Vec::with_capacity(len - 10);
|
|
|
|
vec3.resize(len - 10, 0);
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: slow zero-filling initialization
|
2018-10-18 18:15:48 -05:00
|
|
|
|
2022-06-30 03:50:09 -05:00
|
|
|
let mut vec4 = Vec::with_capacity(len);
|
|
|
|
vec4.resize(vec4.capacity(), 0);
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: slow zero-filling initialization
|
2022-06-30 03:50:09 -05:00
|
|
|
|
2018-10-18 18:15:48 -05:00
|
|
|
// Reinitialization should be warned
|
|
|
|
vec1 = Vec::with_capacity(10);
|
|
|
|
vec1.resize(10, 0);
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: slow zero-filling initialization
|
2018-10-18 18:15:48 -05:00
|
|
|
}
|
2018-10-26 15:35:16 -05:00
|
|
|
|
2023-07-31 16:53:53 -05:00
|
|
|
fn from_empty_vec() {
|
|
|
|
// Resize with constant expression
|
|
|
|
let len = 300;
|
|
|
|
let mut vec1 = Vec::new();
|
|
|
|
vec1.resize(len, 0);
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: slow zero-filling initialization
|
2023-07-31 16:53:53 -05:00
|
|
|
|
|
|
|
// Resize with len expression
|
|
|
|
let mut vec3 = Vec::new();
|
|
|
|
vec3.resize(len - 10, 0);
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: slow zero-filling initialization
|
2023-07-31 16:53:53 -05:00
|
|
|
|
|
|
|
// Reinitialization should be warned
|
|
|
|
vec1 = Vec::new();
|
|
|
|
vec1.resize(10, 0);
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: slow zero-filling initialization
|
2023-09-12 11:13:53 -05:00
|
|
|
|
|
|
|
vec1 = vec![];
|
|
|
|
vec1.resize(10, 0);
|
|
|
|
//~^ ERROR: slow zero-filling initialization
|
|
|
|
|
|
|
|
macro_rules! x {
|
|
|
|
() => {
|
|
|
|
vec![]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// `vec![]` comes from another macro, don't warn
|
|
|
|
vec1 = x!();
|
|
|
|
vec1.resize(10, 0);
|
2023-07-31 16:53:53 -05:00
|
|
|
}
|
|
|
|
|
2022-01-27 08:12:45 -06:00
|
|
|
fn do_stuff(vec: &mut [u8]) {}
|
2018-11-18 16:45:53 -06:00
|
|
|
|
|
|
|
fn extend_vector_with_manipulations_between() {
|
|
|
|
let len = 300;
|
2018-12-09 16:26:16 -06:00
|
|
|
let mut vec1: Vec<u8> = Vec::with_capacity(len);
|
2018-11-18 16:45:53 -06:00
|
|
|
do_stuff(&mut vec1);
|
|
|
|
vec1.extend(repeat(0).take(len));
|
|
|
|
}
|