]> git.lizzy.rs Git - rust.git/blob - tests/ui/slow_vector_initialization.rs
Keep testing unsafe_vector_initialization as ui test
[rust.git] / tests / ui / slow_vector_initialization.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
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.
9
10 #![warn(clippy::unsafe_vector_initialization)]
11
12 use std::iter::repeat;
13
14 fn main() {
15     resize_vector();
16     extend_vector();
17     mixed_extend_resize_vector();
18     unsafe_vector();
19 }
20
21 fn extend_vector() {
22     // Extend with constant expression
23     let len = 300;
24     let mut vec1 = Vec::with_capacity(len);
25     vec1.extend(repeat(0).take(len));
26
27     // Extend with len expression
28     let mut vec2 = Vec::with_capacity(len - 10);
29     vec2.extend(repeat(0).take(len - 10));
30
31     // Extend with mismatching expression should not be warned
32     let mut vec3 = Vec::with_capacity(24322);
33     vec3.extend(repeat(0).take(2));
34 }
35
36 fn mixed_extend_resize_vector() {
37     // Mismatching len
38     let mut mismatching_len = Vec::with_capacity(30);
39     mismatching_len.extend(repeat(0).take(40));
40
41     // Slow initialization
42     let mut resized_vec = Vec::with_capacity(30);
43     resized_vec.resize(30, 0);
44
45     let mut extend_vec = Vec::with_capacity(30);
46     extend_vec.extend(repeat(0).take(30));
47 }
48
49 fn resize_vector() {
50     // Resize with constant expression
51     let len = 300;
52     let mut vec1 = Vec::with_capacity(len);
53     vec1.resize(len, 0);
54
55     // Resize mismatch len
56     let mut vec2 = Vec::with_capacity(200);
57     vec2.resize(10, 0);
58
59     // Resize with len expression
60     let mut vec3 = Vec::with_capacity(len - 10);
61     vec3.resize(len - 10, 0);
62
63     // Reinitialization should be warned
64     vec1 = Vec::with_capacity(10);
65     vec1.resize(10, 0);
66 }
67
68 fn unsafe_vector() {
69     let mut unsafe_vec: Vec<u8> = Vec::with_capacity(200);
70
71     unsafe {
72         unsafe_vec.set_len(200);
73     }
74 }
75
76 fn do_stuff(vec: &mut Vec<u8>) {
77
78 }
79
80 fn extend_vector_with_manipulations_between() {
81     let len = 300;
82     let mut vec1:Vec<u8> = Vec::with_capacity(len);
83     do_stuff(&mut vec1);
84     vec1.extend(repeat(0).take(len));
85 }