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