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