]> git.lizzy.rs Git - rust.git/blob - tests/ui/needless_late_init.rs
Rollup merge of #92849 - flip1995:clippyup, r=Manishearth
[rust.git] / tests / ui / needless_late_init.rs
1 #![allow(unused)]
2
3 fn main() {
4     let a;
5     let n = 1;
6     match n {
7         1 => a = "one",
8         _ => {
9             a = "two";
10         },
11     }
12
13     let b;
14     if n == 3 {
15         b = "four";
16     } else {
17         b = "five"
18     }
19
20     let c;
21     if let Some(n) = Some(5) {
22         c = n;
23     } else {
24         c = -50;
25     }
26
27     let d;
28     if true {
29         let temp = 5;
30         d = temp;
31     } else {
32         d = 15;
33     }
34
35     let e;
36     if true {
37         e = format!("{} {}", a, b);
38     } else {
39         e = format!("{}", c);
40     }
41
42     let f;
43     match 1 {
44         1 => f = "three",
45         _ => return,
46     }; // has semi
47
48     let g: usize;
49     if true {
50         g = 5;
51     } else {
52         panic!();
53     }
54
55     println!("{}", a);
56 }
57
58 async fn in_async() -> &'static str {
59     async fn f() -> &'static str {
60         "one"
61     }
62
63     let a;
64     let n = 1;
65     match n {
66         1 => a = f().await,
67         _ => {
68             a = "two";
69         },
70     }
71
72     a
73 }
74
75 const fn in_const() -> &'static str {
76     const fn f() -> &'static str {
77         "one"
78     }
79
80     let a;
81     let n = 1;
82     match n {
83         1 => a = f(),
84         _ => {
85             a = "two";
86         },
87     }
88
89     a
90 }
91
92 fn does_not_lint() {
93     let z;
94     if false {
95         z = 1;
96     }
97
98     let x;
99     let y;
100     if true {
101         x = 1;
102     } else {
103         y = 1;
104     }
105
106     let mut x;
107     if true {
108         x = 5;
109         x = 10 / x;
110     } else {
111         x = 2;
112     }
113
114     let x;
115     let _ = match 1 {
116         1 => x = 10,
117         _ => x = 20,
118     };
119
120     // using tuples would be possible, but not always preferable
121     let x;
122     let y;
123     if true {
124         x = 1;
125         y = 2;
126     } else {
127         x = 3;
128         y = 4;
129     }
130
131     // could match with a smarter heuristic to avoid multiple assignments
132     let x;
133     if true {
134         let mut y = 5;
135         y = 6;
136         x = y;
137     } else {
138         x = 2;
139     }
140
141     let (x, y);
142     if true {
143         x = 1;
144     } else {
145         x = 2;
146     }
147     y = 3;
148
149     macro_rules! assign {
150         ($i:ident) => {
151             $i = 1;
152         };
153     }
154     let x;
155     assign!(x);
156
157     let x;
158     if true {
159         assign!(x);
160     } else {
161         x = 2;
162     }
163
164     macro_rules! in_macro {
165         () => {
166             let x;
167             x = 1;
168
169             let x;
170             if true {
171                 x = 1;
172             } else {
173                 x = 2;
174             }
175         };
176     }
177     in_macro!();
178
179     println!("{}", x);
180 }