]> git.lizzy.rs Git - rust.git/blob - src/tools/rustfmt/tests/target/expr.rs
Merge commit 'efa8f5521d3813cc897ba29ea0ef98c7aef66bb6' into rustfmt-subtree
[rust.git] / src / tools / rustfmt / 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.source_map.span_to_filename(s) == self.source_map.span_to_filename(m.inner);
15
16     let some_val = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa * bbbb
17         / (bbbbbb - function_call(x, *very_long_pointer, y))
18         + 1000;
19
20     some_ridiculously_loooooooooooooooooooooong_function(
21         10000 * 30000000000 + 40000 / 1002200000000 - 50000 * sqrt(-1),
22         trivial_value,
23     );
24     (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
25         + a
26         + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
27         + aaaaa);
28
29     {
30         for _ in 0..10 {}
31     }
32
33     {
34         {
35             {
36                 {}
37             }
38         }
39     }
40
41     if 1 + 2 > 0 {
42         let result = 5;
43         result
44     } else {
45         4
46     };
47
48     if let Some(x) = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa {
49         // Nothing
50     }
51
52     if let Some(x) =
53         (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)
54     {}
55
56     if let (
57         some_very_large,
58         tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple,
59     ) = 1 + 2 + 3
60     {}
61
62     if let (
63         some_very_large,
64         tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple,
65     ) = 1111 + 2222
66     {}
67
68     if let (
69         some_very_large,
70         tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple,
71     ) = 1 + 2 + 3
72     {}
73
74     if let ast::ItemKind::Trait(_, unsafety, ref generics, ref type_param_bounds, ref trait_items) =
75         item.node
76     {
77         // nothing
78     }
79
80     let test = if true { 5 } else { 3 };
81
82     if cond() {
83         something();
84     } else if different_cond() {
85         something_else();
86     } else {
87         // Check subformatting
88         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
89             + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
90     }
91
92     // #2884
93     let _ = [0; {
94         struct Foo;
95         impl Foo {
96             const fn get(&self) -> usize {
97                 5
98             }
99         };
100         Foo.get()
101     }];
102 }
103
104 fn bar() {
105     let range =
106         (111111111 + 333333333333333333 + 1111 + 400000000000000000)..(2222 + 2333333333333333);
107
108     let another_range = 5..some_func(a, b /* comment */);
109
110     for _ in 1.. {
111         call_forever();
112     }
113
114     syntactically_correct(
115         loop {
116             sup('?');
117         },
118         if cond { 0 } else { 1 },
119     );
120
121     let third = ..10;
122     let infi_range = ..;
123     let foo = 1..;
124     let bar = 5;
125     let nonsense = (10..0)..(0..10);
126
127     loop {
128         if true {
129             break;
130         }
131     }
132
133     let x = (
134         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
135         a,
136     );
137 }
138
139 fn baz() {
140     unsafe /* {}{}{}{{{{}} */ {
141         let foo = 1u32;
142     }
143
144     unsafe /* very looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong
145             * comment */ {
146     }
147
148     unsafe /* So this is a very long comment.
149             * Multi-line, too.
150             * Will it still format correctly? */ {
151     }
152
153     unsafe {
154         // Regular unsafe block
155     }
156
157     unsafe { foo() }
158
159     unsafe {
160         foo();
161     }
162
163     // #2289
164     let identifier_0 = unsafe { this_is_58_chars_long_and_line_is_93_chars_long_xxxxxxxxxx };
165     let identifier_1 = unsafe { this_is_59_chars_long_and_line_is_94_chars_long_xxxxxxxxxxx };
166     let identifier_2 = unsafe { this_is_65_chars_long_and_line_is_100_chars_long_xxxxxxxxxxxxxxxx };
167     let identifier_3 =
168         unsafe { this_is_66_chars_long_and_line_is_101_chars_long_xxxxxxxxxxxxxxxxx };
169 }
170
171 // Test some empty blocks.
172 fn qux() {
173     {}
174     // FIXME this one could be done better.
175     { /* a block with a comment */ }
176     {}
177     {
178         // A block with a comment.
179     }
180 }
181
182 fn issue227() {
183     {
184         let handler =
185             box DocumentProgressHandler::new(addr, DocumentProgressTask::DOMContentLoaded);
186     }
187 }
188
189 fn issue184(source: &str) {
190     for c in source.chars() {
191         if index < 'a' {
192             continue;
193         }
194     }
195 }
196
197 fn arrays() {
198     let x = [
199         0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 7, 8, 9, 0, 1, 2, 3, 4, 5,
200         6, 7, 8, 9, 0,
201     ];
202
203     let y = [/* comment */ 1, 2 /* post comment */, 3];
204
205     let xy = [
206         strukt {
207             test123: value_one_two_three_four,
208             turbo: coolio(),
209         },
210         // comment
211         1,
212     ];
213
214     let a = WeightedChoice::new(&mut [
215         Weighted {
216             weightweight: x,
217             item: 0,
218         },
219         Weighted {
220             weightweight: 1,
221             item: 1,
222         },
223         Weighted {
224             weightweight: x,
225             item: 2,
226         },
227         Weighted {
228             weightweight: 1,
229             item: 3,
230         },
231     ]);
232
233     let z = [
234         xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
235         yyyyyyyyyyyyyyyyyyyyyyyyyyy,
236         zzzzzzzzzzzzzzzzzz,
237         q,
238     ];
239
240     [1 + 3, 4, 5, 6, 7, 7, fncall::<Vec<_>>(3 - 1)]
241 }
242
243 fn returns() {
244     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
245         && return;
246
247     return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
248         + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
249 }
250
251 fn addrof() {
252     &mut (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
253         + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
254     &(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
255         + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
256
257     // raw reference operator
258     &raw const a;
259     &raw mut b;
260 }
261
262 fn casts() {
263     fn unpack(packed: u32) -> [u16; 2] {
264         [(packed >> 16) as u16, (packed >> 0) as u16]
265     }
266
267     let some_trait_xxx = xxxxxxxxxxx + xxxxxxxxxxxxx as SomeTraitXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX;
268     let slightly_longer_trait =
269         yyyyyyyyy + yyyyyyyyyyy as SomeTraitYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY;
270 }
271
272 fn indices() {
273     let x = (aaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccc)
274         [x + y + z];
275     let y = (aaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccc)
276         [xxxxx + yyyyy + zzzzz];
277     let z = xxxxxxxxxx
278         .x()
279         .y()
280         .zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz()[aaaaa];
281     let z = xxxxxxxxxx
282         .x()
283         .y()
284         .zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz()
285         [aaaaa];
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 because inclusive ranges need a defined end
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) = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
341     {
342         ha();
343     } else if xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxxx
344     {
345         yo();
346     }
347 }
348
349 fn issue1106() {
350     {
351         if let hir::ItemEnum(ref enum_def, ref generics) =
352             self.ast_map.expect_item(enum_node_id).node
353         {}
354     }
355
356     for entry in WalkDir::new(path)
357         .into_iter()
358         .filter_entry(|entry| exclusions.filter_entry(entry))
359     {}
360 }
361
362 fn issue1570() {
363     a_very_long_function_name({ some_func(1, { 1 }) })
364 }
365
366 fn issue1714() {
367     v = &mut { v }[mid..];
368     let (left, right) = { v }.split_at_mut(mid);
369 }
370
371 // Multi-lined index should be put on the next line if it fits in one line.
372 fn issue1749() {
373     {
374         {
375             {
376                 if self.shape[(r as f32 + self.x_offset) as usize]
377                     [(c as f32 + self.y_offset) as usize]
378                     != 0
379                 {
380                     // hello
381                 }
382             }
383         }
384     }
385 }
386
387 // #1172
388 fn newlines_between_list_like_expr() {
389     foo(
390         xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
391         yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,
392         zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz,
393     );
394
395     vec![
396         xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
397         yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,
398         zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz,
399     ];
400
401     match x {
402         xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
403         | yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
404         | zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz => foo(a, b, c),
405         _ => bar(),
406     };
407 }
408
409 fn issue2178() {
410     Ok(result
411         .iter()
412         .map(|item| ls_util::rls_to_location(item))
413         .collect())
414 }
415
416 // #2493
417 impl Foo {
418     fn bar(&self) {
419         {
420             let x = match () {
421                 () => {
422                     let i;
423                     i == self
424                         .install_config
425                         .storage
426                         .experimental_compressed_block_size as usize
427                 }
428             };
429         }
430     }
431 }
432
433 fn dots() {
434     .. .. ..; // (.. (.. (..)))
435     ..= ..= ..;
436     (..).. ..; // ((..) .. (..))
437 }
438
439 // #2676
440 // A function call with a large single argument.
441 fn foo() {
442     let my_var = Mutex::new(
443         RpcClientType::connect(server_iddd).chain_err(|| "Unable to create RPC client")?,
444     );
445 }
446
447 // #2704
448 // Method call with prefix and suffix.
449 fn issue2704() {
450     // We should not combine the callee with a multi-lined method call.
451     let requires = requires.set(
452         &requires0
453             .concat(&requires1)
454             .concat(&requires2)
455             .distinct_total(),
456     );
457     let requires = requires.set(
458         box requires0
459             .concat(&requires1)
460             .concat(&requires2)
461             .distinct_total(),
462     );
463     let requires = requires.set(
464         requires0
465             .concat(&requires1)
466             .concat(&requires2)
467             .distinct_total() as u32,
468     );
469     let requires = requires.set(
470         requires0
471             .concat(&requires1)
472             .concat(&requires2)
473             .distinct_total()?,
474     );
475     let requires = requires.set(
476         !requires0
477             .concat(&requires1)
478             .concat(&requires2)
479             .distinct_total(),
480     );
481     // We should combine a small callee with an argument.
482     bar(vec![22]
483         .into_iter()
484         .map(|x| x * 2)
485         .filter(|_| true)
486         .collect());
487     // But we should not combine a long callee with an argument.
488     barrrr(
489         vec![22]
490             .into_iter()
491             .map(|x| x * 2)
492             .filter(|_| true)
493             .collect(),
494     );
495 }
496
497 // #2782
498 fn issue2782() {
499     {
500         let f = {
501             let f = {
502                 {
503                     match f {
504                         F(f, _) => loop {
505                             let f = {
506                                 match f {
507                                     F(f, _) => match f {
508                                         F(f, _) => loop {
509                                             let f = {
510                                                 let f = {
511                                                     match f {
512                                                         '-' => F(f, ()),
513                                                     }
514                                                 };
515                                             };
516                                         },
517                                     },
518                                 }
519                             };
520                         },
521                     }
522                 }
523             };
524         };
525     }
526 }
527
528 fn issue_2802() {
529     function_to_fill_this_line(some_arg, some_arg, some_arg)
530         * a_very_specific_length(specific_length_arg)
531         * very_specific_length(Foo {
532             a: some_much_much_longer_value,
533         })
534         * some_value
535 }
536
537 fn issue_3003() {
538     let mut path: PathBuf = [
539         env!("CARGO_MANIFEST_DIR"),
540         "tests",
541         "support",
542         "dejavu-fonts-ttf-2.37",
543         "ttf",
544     ]
545     .iter()
546     .collect();
547 }
548
549 fn issue3226() {
550     {
551         {
552             {
553                 return Err(
554                     ErrorKind::ManagementInterfaceError("Server exited unexpectedly").into(),
555                 );
556             }
557         }
558     }
559     {
560         {
561             {
562                 break Err(
563                     ErrorKind::ManagementInterfaceError("Server exited unexpectedlyy").into(),
564                 );
565             }
566         }
567     }
568 }
569
570 // #3457
571 fn issue3457() {
572     {
573         {
574             {
575                 {
576                     {
577                         {
578                             {
579                                 {
580                                     {
581                                         {
582                                             {
583                                                 {
584                                                     {
585                                                         {
586                                                             {
587                                                                 {
588                                                                     {
589                                                                         {
590                                                                             {
591                                                                                 {
592                                                                                     {
593                                                                                         {
594                                                                                             {
595                                                                                                 {
596                                                                                                     println!("Test");
597                                                                                                 }
598                                                                                             }
599                                                                                         }
600                                                                                     }
601                                                                                 }
602                                                                             }
603                                                                         }
604                                                                     }
605                                                                 }
606                                                             }
607                                                         }
608                                                     }
609                                                 }
610                                             }
611                                         }
612                                     }
613                                 }
614                             }
615                         }
616                     }
617                 }
618             }
619         }
620     }
621 }
622
623 // #3498
624 static REPRO: &[usize] = &[
625     #[cfg(feature = "zero")]
626     0,
627 ];
628
629 fn overflow_with_attr() {
630     foo(
631         #[cfg(feature = "zero")]
632         0,
633     );
634     foobar(
635         #[cfg(feature = "zero")]
636         0,
637     );
638     foobar(
639         x,
640         y,
641         #[cfg(feature = "zero")]
642         {},
643     );
644 }
645
646 // https://github.com/rust-lang/rustfmt/issues/3765
647 fn foo() {
648     async {
649         // Do
650         // some
651         // work
652     }
653     .await;
654
655     async {
656         // Do
657         // some
658         // work
659     }
660     .await;
661 }
662
663 fn underscore() {
664     _ = 1;
665     _;
666     [_, a, _] = [1, 2, 3];
667     (a, _) = (8, 9);
668     TupleStruct(_, a) = TupleStruct(2, 2);
669
670     let _: usize = foo(_, _);
671 }