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