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