]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/needless_late_init.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / needless_late_init.rs
1 // run-rustfix
2 #![feature(let_chains)]
3 #![allow(
4 unused,
5 clippy::assign_op_pattern,
6 clippy::blocks_in_if_conditions,
7 clippy::let_and_return,
8 clippy::let_unit_value,
9 clippy::nonminimal_bool
10 )]
11
12 use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
13 use std::rc::Rc;
14
15 struct SignificantDrop;
16 impl std::ops::Drop for SignificantDrop {
17 fn drop(&mut self) {
18 println!("dropped");
19 }
20 }
21
22 fn simple() {
23 let a;
24 a = "zero";
25
26 let b;
27 let c;
28 b = 1;
29 c = 2;
30
31 let d: usize;
32 d = 1;
33
34 let e;
35 e = format!("{}", d);
36 }
37
38 fn main() {
39 let a;
40 let n = 1;
41 match n {
42 1 => a = "one",
43 _ => {
44 a = "two";
45 },
46 }
47
48 let b;
49 if n == 3 {
50 b = "four";
51 } else {
52 b = "five"
53 }
54
55 let d;
56 if true {
57 let temp = 5;
58 d = temp;
59 } else {
60 d = 15;
61 }
62
63 let e;
64 if true {
65 e = format!("{} {}", a, b);
66 } else {
67 e = format!("{}", n);
68 }
69
70 let f;
71 match 1 {
72 1 => f = "three",
73 _ => return,
74 }; // has semi
75
76 let g: usize;
77 if true {
78 g = 5;
79 } else {
80 panic!();
81 }
82
83 // Drop order only matters if both are significant
84 let x;
85 let y = SignificantDrop;
86 x = 1;
87
88 let x;
89 let y = 1;
90 x = SignificantDrop;
91
92 let x;
93 // types that should be considered insignificant
94 let y = 1;
95 let y = "2";
96 let y = String::new();
97 let y = vec![3.0];
98 let y = HashMap::<usize, usize>::new();
99 let y = BTreeMap::<usize, usize>::new();
100 let y = HashSet::<usize>::new();
101 let y = BTreeSet::<usize>::new();
102 let y = Box::new(4);
103 x = SignificantDrop;
104 }
105
106 async fn in_async() -> &'static str {
107 async fn f() -> &'static str {
108 "one"
109 }
110
111 let a;
112 let n = 1;
113 match n {
114 1 => a = f().await,
115 _ => {
116 a = "two";
117 },
118 }
119
120 a
121 }
122
123 const fn in_const() -> &'static str {
124 const fn f() -> &'static str {
125 "one"
126 }
127
128 let a;
129 let n = 1;
130 match n {
131 1 => a = f(),
132 _ => {
133 a = "two";
134 },
135 }
136
137 a
138 }
139
140 fn does_not_lint() {
141 let z;
142 if false {
143 z = 1;
144 }
145
146 let x;
147 let y;
148 if true {
149 x = 1;
150 } else {
151 y = 1;
152 }
153
154 let mut x;
155 if true {
156 x = 5;
157 x = 10 / x;
158 } else {
159 x = 2;
160 }
161
162 let x;
163 let _ = match 1 {
164 1 => x = 10,
165 _ => x = 20,
166 };
167
168 // using tuples would be possible, but not always preferable
169 let x;
170 let y;
171 if true {
172 x = 1;
173 y = 2;
174 } else {
175 x = 3;
176 y = 4;
177 }
178
179 // could match with a smarter heuristic to avoid multiple assignments
180 let x;
181 if true {
182 let mut y = 5;
183 y = 6;
184 x = y;
185 } else {
186 x = 2;
187 }
188
189 let (x, y);
190 if true {
191 x = 1;
192 } else {
193 x = 2;
194 }
195 y = 3;
196
197 macro_rules! assign {
198 ($i:ident) => {
199 $i = 1;
200 };
201 }
202 let x;
203 assign!(x);
204
205 let x;
206 if true {
207 assign!(x);
208 } else {
209 x = 2;
210 }
211
212 macro_rules! in_macro {
213 () => {
214 let x;
215 x = 1;
216
217 let x;
218 if true {
219 x = 1;
220 } else {
221 x = 2;
222 }
223 };
224 }
225 in_macro!();
226
227 // ignore if-lets - https://github.com/rust-lang/rust-clippy/issues/8613
228 let x;
229 if let Some(n) = Some("v") {
230 x = 1;
231 } else {
232 x = 2;
233 }
234
235 let x;
236 if true && let Some(n) = Some("let chains too") {
237 x = 1;
238 } else {
239 x = 2;
240 }
241
242 // ignore mut bindings
243 // https://github.com/shepmaster/twox-hash/blob/b169c16d86eb8ea4a296b0acb9d00ca7e3c3005f/src/sixty_four.rs#L88-L93
244 // https://github.com/dtolnay/thiserror/blob/21c26903e29cb92ba1a7ff11e82ae2001646b60d/tests/test_generics.rs#L91-L100
245 let mut x: usize;
246 x = 1;
247 x = 2;
248 x = 3;
249
250 // should not move the declaration if `x` has a significant drop, and there
251 // is another binding with a significant drop between it and the first usage
252 let x;
253 let y = SignificantDrop;
254 x = SignificantDrop;
255 }
256
257 #[rustfmt::skip]
258 fn issue8911() -> u32 {
259 let x;
260 match 1 {
261 _ if { x = 1; false } => return 1,
262 _ => return 2,
263 }
264
265 let x;
266 if { x = 1; true } {
267 return 1;
268 } else {
269 return 2;
270 }
271
272 3
273 }