]> git.lizzy.rs Git - rust.git/blob - tests/ui/vec_init_then_push.rs
Fix `unnecessary_cast` suggestion when taking a reference
[rust.git] / tests / ui / vec_init_then_push.rs
1 #![allow(unused_variables)]
2 #![warn(clippy::vec_init_then_push)]
3
4 fn main() {
5     let mut def_err: Vec<u32> = Default::default();
6     def_err.push(0);
7
8     let mut new_err = Vec::<u32>::new();
9     new_err.push(1);
10
11     let mut cap_err = Vec::with_capacity(2);
12     cap_err.push(0);
13     cap_err.push(1);
14     cap_err.push(2);
15     if true {
16         // don't include this one
17         cap_err.push(3);
18     }
19
20     let mut cap_ok = Vec::with_capacity(10);
21     cap_ok.push(0);
22
23     new_err = Vec::new();
24     new_err.push(0);
25
26     let mut vec = Vec::new();
27     // control flow at block final expression
28     if true {
29         // no lint
30         vec.push(1);
31     }
32
33     let mut vec = Vec::with_capacity(5);
34     vec.push(1);
35     vec.push(2);
36     vec.push(3);
37     vec.push(4);
38 }
39
40 pub fn no_lint() -> Vec<i32> {
41     let mut p = Some(1);
42     let mut vec = Vec::new();
43     loop {
44         match p {
45             None => return vec,
46             Some(i) => {
47                 vec.push(i);
48                 p = None;
49             },
50         }
51     }
52 }
53
54 fn _from_iter(items: impl Iterator<Item = u32>) -> Vec<u32> {
55     let mut v = Vec::new();
56     v.push(0);
57     v.push(1);
58     v.extend(items);
59     v
60 }
61
62 fn _cond_push(x: bool) -> Vec<u32> {
63     let mut v = Vec::new();
64     v.push(0);
65     if x {
66         v.push(1);
67     }
68     v.push(2);
69     v
70 }
71
72 fn _push_then_edit(x: u32) -> Vec<u32> {
73     let mut v = Vec::new();
74     v.push(x);
75     v.push(1);
76     v[0] = v[1] + 5;
77     v
78 }
79
80 fn _cond_push_with_large_start(x: bool) -> Vec<u32> {
81     let mut v = Vec::new();
82     v.push(0);
83     v.push(1);
84     v.push(0);
85     v.push(1);
86     v.push(0);
87     v.push(0);
88     v.push(1);
89     v.push(0);
90     if x {
91         v.push(1);
92     }
93
94     let mut v2 = Vec::new();
95     v2.push(0);
96     v2.push(1);
97     v2.push(0);
98     v2.push(1);
99     v2.push(0);
100     v2.push(0);
101     v2.push(1);
102     v2.push(0);
103     v2.extend(&v);
104
105     v2
106 }
107
108 fn f() {
109     let mut v = Vec::new();
110     v.push((0i32, 0i32));
111     let y = v[0].0.abs();
112 }