]> git.lizzy.rs Git - rust.git/blob - tests/target/expr.rs
Format source codes
[rust.git] / tests / target / expr.rs
1 // rustfmt-normalize_comments: true
2 // rustfmt-wrap_comments: true
3 // Test expressions
4
5 fn foo() -> bool {
6     let boxed: Box<i32> = box 5;
7     let referenced = &5;
8
9     let very_long_variable_name = (a + first + simple + test);
10     let very_long_variable_name =
11         (a + first + simple + test + AAAAAAAAAAAAA + BBBBBBBBBBBBBBBBB + b + c);
12
13     let is_internalxxxx =
14         self.codemap.span_to_filename(s) == self.codemap.span_to_filename(m.inner);
15
16     let some_val = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa * bbbb /
17         (bbbbbb - function_call(x, *very_long_pointer, y)) + 1000;
18
19     some_ridiculously_loooooooooooooooooooooong_function(
20         10000 * 30000000000 + 40000 / 1002200000000 - 50000 * sqrt(-1),
21         trivial_value,
22     );
23     (((((((((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
24                  a +
25                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
26                  aaaaa)))))))));
27
28     {
29         for _ in 0..10 {}
30     }
31
32     {
33         {
34             {
35                 {}
36             }
37         }
38     }
39
40     if 1 + 2 > 0 {
41         let result = 5;
42         result
43     } else {
44         4
45     };
46
47     if let Some(x) = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa {
48         // Nothing
49     }
50
51     if let Some(x) = (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
52                           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)
53     {}
54
55     if let (
56         some_very_large,
57         tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple,
58     ) = 1 + 2 + 3
59     {}
60
61     if let (
62         some_very_large,
63         tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple,
64     ) = 1111 + 2222
65     {}
66
67     if let (
68         some_very_large,
69         tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple,
70     ) = 1 + 2 + 3
71     {}
72
73     let test = if true { 5 } else { 3 };
74
75     if cond() {
76         something();
77     } else if different_cond() {
78         something_else();
79     } else {
80         // Check subformatting
81         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
82             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
83     }
84 }
85
86 fn bar() {
87     let range =
88         (111111111 + 333333333333333333 + 1111 + 400000000000000000)..(2222 + 2333333333333333);
89
90     let another_range = 5..some_func(a, b /* comment */);
91
92     for _ in 1.. {
93         call_forever();
94     }
95
96     syntactically_correct(
97         loop {
98             sup('?');
99         },
100         if cond { 0 } else { 1 },
101     );
102
103     let third = ..10;
104     let infi_range = ..;
105     let foo = 1..;
106     let bar = 5;
107     let nonsense = (10..0)..(0..10);
108
109     loop {
110         if true {
111             break;
112         }
113     }
114
115     let x = (
116         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
117         a,
118     );
119 }
120
121 fn baz() {
122     unsafe /* {}{}{}{{{{}} */ {
123         let foo = 1u32;
124     }
125
126     unsafe /* very looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong
127             * comment */ {
128     }
129
130     unsafe /* So this is a very long comment.
131             * Multi-line, too.
132             * Will it still format correctly? */ {
133     }
134
135     unsafe {
136         // Regular unsafe block
137     }
138
139     unsafe { foo() }
140
141     unsafe {
142         foo();
143     }
144 }
145
146 // Test some empty blocks.
147 fn qux() {
148     {}
149     // FIXME this one could be done better.
150     { /* a block with a comment */ }
151     {}
152     {
153         // A block with a comment.
154     }
155 }
156
157 fn issue227() {
158     {
159         let handler =
160             box DocumentProgressHandler::new(addr, DocumentProgressTask::DOMContentLoaded);
161     }
162 }
163
164 fn issue184(source: &str) {
165     for c in source.chars() {
166         if index < 'a' {
167             continue;
168         }
169     }
170 }
171
172 fn arrays() {
173     let x = [
174         0,
175         1,
176         2,
177         3,
178         4,
179         5,
180         6,
181         7,
182         8,
183         9,
184         0,
185         1,
186         2,
187         3,
188         4,
189         5,
190         6,
191         7,
192         8,
193         9,
194         0,
195         7,
196         8,
197         9,
198         0,
199         1,
200         2,
201         3,
202         4,
203         5,
204         6,
205         7,
206         8,
207         9,
208         0,
209     ];
210
211     let y = [
212         // comment
213         1,
214         2, // post comment
215         3,
216     ];
217
218     let xy = [
219         strukt {
220             test123: value_one_two_three_four,
221             turbo: coolio(),
222         },
223         // comment
224         1,
225     ];
226
227     let a = WeightedChoice::new(&mut [
228         Weighted {
229             weightweight: x,
230             item: 0,
231         },
232         Weighted {
233             weightweight: 1,
234             item: 1,
235         },
236         Weighted {
237             weightweight: x,
238             item: 2,
239         },
240         Weighted {
241             weightweight: 1,
242             item: 3,
243         },
244     ]);
245
246     let z = [
247         xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
248         yyyyyyyyyyyyyyyyyyyyyyyyyyy,
249         zzzzzzzzzzzzzzzzzz,
250         q,
251     ];
252
253     [1 + 3, 4, 5, 6, 7, 7, fncall::<Vec<_>>(3 - 1)]
254 }
255
256 fn returns() {
257     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&
258         return;
259
260     return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
261         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
262 }
263
264 fn addrof() {
265     &mut (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
266               bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
267     &(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
268           bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
269 }
270
271 fn casts() {
272     fn unpack(packed: u32) -> [u16; 2] {
273         [(packed >> 16) as u16, (packed >> 0) as u16]
274     }
275
276     let some_trait_xxx = xxxxxxxxxxx + xxxxxxxxxxxxx as SomeTraitXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX;
277     let slightly_longer_trait =
278         yyyyyyyyy + yyyyyyyyyyy as SomeTraitYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY;
279 }
280
281 fn indices() {
282     let x = (aaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccc)
283         [x + y + z];
284     let y = (aaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccc)
285         [xxxxx + yyyyy + zzzzz];
286 }
287
288 fn repeats() {
289     let x = [aaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccc;
290         x + y + z];
291     let y = [aaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccc;
292         xxxxx + yyyyy + zzzzz];
293 }
294
295 fn blocks() {
296     if 1 + 1 == 2 {
297         println!("yay arithmetix!");
298     };
299 }
300
301 fn issue767() {
302     if false {
303         if false {
304         } else {
305             // A let binding here seems necessary to trigger it.
306             let _ = ();
307         }
308     } else if let false = false {
309     }
310 }
311
312 fn ranges() {
313     let x = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
314     let y =
315         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
316     let z = ...x;
317
318     // #1766
319     let x = [0. ..10.0];
320     let x = [0. ...10.0];
321
322     a...b
323
324     // the expr below won't compile for some reason...
325     // let a = 0 ... ;
326 }
327
328 fn if_else() {
329     let exact = diff / (if size == 0 { 1 } else { size });
330
331     let cx = tp1.x + any * radius * if anticlockwise { 1.0 } else { -1.0 };
332 }
333
334 fn complex_if_else() {
335     if let Some(x) = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {
336     } else if let Some(x) = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {
337         ha();
338     } else if xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxx {
339         yo();
340     } else if let Some(x) =
341         xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
342     {
343         ha();
344     } else if xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +
345                xxxxxxxxx
346     {
347         yo();
348     }
349 }
350
351 fn issue1106() {
352     {
353         if let hir::ItemEnum(ref enum_def, ref generics) =
354             self.ast_map.expect_item(enum_node_id).node
355         {}
356     }
357
358     for entry in WalkDir::new(path)
359         .into_iter()
360         .filter_entry(|entry| exclusions.filter_entry(entry))
361     {}
362 }
363
364 fn issue1570() {
365     a_very_long_function_name({ some_func(1, { 1 }) })
366 }
367
368 fn issue1714() {
369     v = &mut { v }[mid..];
370     let (left, right) = { v }.split_at_mut(mid);
371 }
372
373 // Multi-lined index should be put on the next line if it fits in one line.
374 fn issue1749() {
375     {
376         {
377             {
378                 if self.shape[(r as f32 + self.x_offset) as usize]
379                     [(c as f32 + self.y_offset) as usize] != 0
380                 {
381                     // hello
382                 }
383             }
384         }
385     }
386 }