1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 #![warn(clippy::unsafe_vector_initialization)]
12 use std::iter::repeat;
17 mixed_extend_resize_vector();
22 // Extend with constant expression
24 let mut vec1 = Vec::with_capacity(len);
25 vec1.extend(repeat(0).take(len));
27 // Extend with len expression
28 let mut vec2 = Vec::with_capacity(len - 10);
29 vec2.extend(repeat(0).take(len - 10));
31 // Extend with mismatching expression should not be warned
32 let mut vec3 = Vec::with_capacity(24322);
33 vec3.extend(repeat(0).take(2));
36 fn mixed_extend_resize_vector() {
38 let mut mismatching_len = Vec::with_capacity(30);
39 mismatching_len.extend(repeat(0).take(40));
41 // Slow initialization
42 let mut resized_vec = Vec::with_capacity(30);
43 resized_vec.resize(30, 0);
45 let mut extend_vec = Vec::with_capacity(30);
46 extend_vec.extend(repeat(0).take(30));
50 // Resize with constant expression
52 let mut vec1 = Vec::with_capacity(len);
55 // Resize mismatch len
56 let mut vec2 = Vec::with_capacity(200);
59 // Resize with len expression
60 let mut vec3 = Vec::with_capacity(len - 10);
61 vec3.resize(len - 10, 0);
63 // Reinitialization should be warned
64 vec1 = Vec::with_capacity(10);
69 let mut unsafe_vec: Vec<u8> = Vec::with_capacity(200);
72 unsafe_vec.set_len(200);
76 fn do_stuff(vec: &mut Vec<u8>) {
80 fn extend_vector_with_manipulations_between() {
82 let mut vec1:Vec<u8> = Vec::with_capacity(len);
84 vec1.extend(repeat(0).take(len));