]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/needless_late_init.rs
Rollup merge of #99396 - compiler-errors:missing-tests, r=Mark-Simulacrum
[rust.git] / src / tools / clippy / tests / ui / needless_late_init.rs
1 // run-rustfix
2 #![allow(
3     unused,
4     clippy::assign_op_pattern,
5     clippy::blocks_in_if_conditions,
6     clippy::let_and_return,
7     clippy::let_unit_value,
8     clippy::nonminimal_bool
9 )]
10
11 use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
12 use std::rc::Rc;
13
14 struct SignificantDrop;
15 impl std::ops::Drop for SignificantDrop {
16     fn drop(&mut self) {
17         println!("dropped");
18     }
19 }
20
21 fn simple() {
22     let a;
23     a = "zero";
24
25     let b;
26     let c;
27     b = 1;
28     c = 2;
29
30     let d: usize;
31     d = 1;
32
33     let e;
34     e = format!("{}", d);
35 }
36
37 fn main() {
38     let a;
39     let n = 1;
40     match n {
41         1 => a = "one",
42         _ => {
43             a = "two";
44         },
45     }
46
47     let b;
48     if n == 3 {
49         b = "four";
50     } else {
51         b = "five"
52     }
53
54     let d;
55     if true {
56         let temp = 5;
57         d = temp;
58     } else {
59         d = 15;
60     }
61
62     let e;
63     if true {
64         e = format!("{} {}", a, b);
65     } else {
66         e = format!("{}", n);
67     }
68
69     let f;
70     match 1 {
71         1 => f = "three",
72         _ => return,
73     }; // has semi
74
75     let g: usize;
76     if true {
77         g = 5;
78     } else {
79         panic!();
80     }
81
82     // Drop order only matters if both are significant
83     let x;
84     let y = SignificantDrop;
85     x = 1;
86
87     let x;
88     let y = 1;
89     x = SignificantDrop;
90
91     let x;
92     // types that should be considered insignificant
93     let y = 1;
94     let y = "2";
95     let y = String::new();
96     let y = vec![3.0];
97     let y = HashMap::<usize, usize>::new();
98     let y = BTreeMap::<usize, usize>::new();
99     let y = HashSet::<usize>::new();
100     let y = BTreeSet::<usize>::new();
101     let y = Box::new(4);
102     x = SignificantDrop;
103 }
104
105 async fn in_async() -> &'static str {
106     async fn f() -> &'static str {
107         "one"
108     }
109
110     let a;
111     let n = 1;
112     match n {
113         1 => a = f().await,
114         _ => {
115             a = "two";
116         },
117     }
118
119     a
120 }
121
122 const fn in_const() -> &'static str {
123     const fn f() -> &'static str {
124         "one"
125     }
126
127     let a;
128     let n = 1;
129     match n {
130         1 => a = f(),
131         _ => {
132             a = "two";
133         },
134     }
135
136     a
137 }
138
139 fn does_not_lint() {
140     let z;
141     if false {
142         z = 1;
143     }
144
145     let x;
146     let y;
147     if true {
148         x = 1;
149     } else {
150         y = 1;
151     }
152
153     let mut x;
154     if true {
155         x = 5;
156         x = 10 / x;
157     } else {
158         x = 2;
159     }
160
161     let x;
162     let _ = match 1 {
163         1 => x = 10,
164         _ => x = 20,
165     };
166
167     // using tuples would be possible, but not always preferable
168     let x;
169     let y;
170     if true {
171         x = 1;
172         y = 2;
173     } else {
174         x = 3;
175         y = 4;
176     }
177
178     // could match with a smarter heuristic to avoid multiple assignments
179     let x;
180     if true {
181         let mut y = 5;
182         y = 6;
183         x = y;
184     } else {
185         x = 2;
186     }
187
188     let (x, y);
189     if true {
190         x = 1;
191     } else {
192         x = 2;
193     }
194     y = 3;
195
196     macro_rules! assign {
197         ($i:ident) => {
198             $i = 1;
199         };
200     }
201     let x;
202     assign!(x);
203
204     let x;
205     if true {
206         assign!(x);
207     } else {
208         x = 2;
209     }
210
211     macro_rules! in_macro {
212         () => {
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     in_macro!();
225
226     // ignore if-lets - https://github.com/rust-lang/rust-clippy/issues/8613
227     let x;
228     if let Some(n) = Some("v") {
229         x = 1;
230     } else {
231         x = 2;
232     }
233
234     let x;
235     if true && let Some(n) = Some("let chains too") {
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 }