]> git.lizzy.rs Git - rust.git/blob - tests/target/expr.rs
Merge pull request #3522 from topecongiro/issue-3521
[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.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
258 fn casts() {
259     fn unpack(packed: u32) -> [u16; 2] {
260         [(packed >> 16) as u16, (packed >> 0) as u16]
261     }
262
263     let some_trait_xxx = xxxxxxxxxxx + xxxxxxxxxxxxx as SomeTraitXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX;
264     let slightly_longer_trait =
265         yyyyyyyyy + yyyyyyyyyyy as SomeTraitYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY;
266 }
267
268 fn indices() {
269     let x = (aaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccc)
270         [x + y + z];
271     let y = (aaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccc)
272         [xxxxx + yyyyy + zzzzz];
273     let z = xxxxxxxxxx
274         .x()
275         .y()
276         .zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz()[aaaaa];
277     let z = xxxxxxxxxx
278         .x()
279         .y()
280         .zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz()
281         [aaaaa];
282 }
283
284 fn repeats() {
285     let x = [aaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccc;
286         x + y + z];
287     let y = [aaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccc;
288         xxxxx + yyyyy + zzzzz];
289 }
290
291 fn blocks() {
292     if 1 + 1 == 2 {
293         println!("yay arithmetix!");
294     };
295 }
296
297 fn issue767() {
298     if false {
299         if false {
300         } else {
301             // A let binding here seems necessary to trigger it.
302             let _ = ();
303         }
304     } else if let false = false {
305     }
306 }
307
308 fn ranges() {
309     let x = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
310     let y =
311         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
312     let z = ..=x;
313
314     // #1766
315     let x = [0. ..10.0];
316     let x = [0. ..=10.0];
317
318     a..=b
319
320     // the expr below won't compile because inclusive ranges need a defined end
321     // let a = 0 ..= ;
322 }
323
324 fn if_else() {
325     let exact = diff / (if size == 0 { 1 } else { size });
326
327     let cx = tp1.x + any * radius * if anticlockwise { 1.0 } else { -1.0 };
328 }
329
330 fn complex_if_else() {
331     if let Some(x) = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {
332     } else if let Some(x) = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {
333         ha();
334     } else if xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxx {
335         yo();
336     } else if let Some(x) = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
337     {
338         ha();
339     } else if xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxxx
340     {
341         yo();
342     }
343 }
344
345 fn issue1106() {
346     {
347         if let hir::ItemEnum(ref enum_def, ref generics) =
348             self.ast_map.expect_item(enum_node_id).node
349         {}
350     }
351
352     for entry in WalkDir::new(path)
353         .into_iter()
354         .filter_entry(|entry| exclusions.filter_entry(entry))
355     {}
356 }
357
358 fn issue1570() {
359     a_very_long_function_name({ some_func(1, { 1 }) })
360 }
361
362 fn issue1714() {
363     v = &mut { v }[mid..];
364     let (left, right) = { v }.split_at_mut(mid);
365 }
366
367 // Multi-lined index should be put on the next line if it fits in one line.
368 fn issue1749() {
369     {
370         {
371             {
372                 if self.shape[(r as f32 + self.x_offset) as usize]
373                     [(c as f32 + self.y_offset) as usize]
374                     != 0
375                 {
376                     // hello
377                 }
378             }
379         }
380     }
381 }
382
383 // #1172
384 fn newlines_between_list_like_expr() {
385     foo(
386         xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
387         yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,
388         zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz,
389     );
390
391     vec![
392         xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
393         yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,
394         zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz,
395     ];
396
397     match x {
398         xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
399         | yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
400         | zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz => foo(a, b, c),
401         _ => bar(),
402     };
403 }
404
405 fn issue2178() {
406     Ok(result
407         .iter()
408         .map(|item| ls_util::rls_to_location(item))
409         .collect())
410 }
411
412 // #2493
413 impl Foo {
414     fn bar(&self) {
415         {
416             let x = match () {
417                 () => {
418                     let i;
419                     i == self
420                         .install_config
421                         .storage
422                         .experimental_compressed_block_size as usize
423                 }
424             };
425         }
426     }
427 }
428
429 fn dots() {
430     .. .. ..; // (.. (.. (..)))
431     ..= ..= ..;
432     (..).. ..; // ((..) .. (..))
433 }
434
435 // #2676
436 // A function call with a large single argument.
437 fn foo() {
438     let my_var = Mutex::new(
439         RpcClientType::connect(server_iddd).chain_err(|| "Unable to create RPC client")?,
440     );
441 }
442
443 // #2704
444 // Method call with prefix and suffix.
445 fn issue2704() {
446     // We should not combine the callee with a multi-lined method call.
447     let requires = requires.set(
448         &requires0
449             .concat(&requires1)
450             .concat(&requires2)
451             .distinct_total(),
452     );
453     let requires = requires.set(
454         box requires0
455             .concat(&requires1)
456             .concat(&requires2)
457             .distinct_total(),
458     );
459     let requires = requires.set(
460         requires0
461             .concat(&requires1)
462             .concat(&requires2)
463             .distinct_total() as u32,
464     );
465     let requires = requires.set(
466         requires0
467             .concat(&requires1)
468             .concat(&requires2)
469             .distinct_total()?,
470     );
471     let requires = requires.set(
472         !requires0
473             .concat(&requires1)
474             .concat(&requires2)
475             .distinct_total(),
476     );
477     // We should combine a small callee with an argument.
478     bar(vec![22]
479         .into_iter()
480         .map(|x| x * 2)
481         .filter(|_| true)
482         .collect());
483     // But we should not combine a long callee with an argument.
484     barrrr(
485         vec![22]
486             .into_iter()
487             .map(|x| x * 2)
488             .filter(|_| true)
489             .collect(),
490     );
491 }
492
493 // #2782
494 fn issue2782() {
495     {
496         let f = {
497             let f = {
498                 {
499                     match f {
500                         F(f, _) => loop {
501                             let f = {
502                                 match f {
503                                     F(f, _) => match f {
504                                         F(f, _) => loop {
505                                             let f = {
506                                                 let f = {
507                                                     match f {
508                                                         '-' => F(f, ()),
509                                                     }
510                                                 };
511                                             };
512                                         },
513                                     },
514                                 }
515                             };
516                         },
517                     }
518                 }
519             };
520         };
521     }
522 }
523
524 fn issue_2802() {
525     function_to_fill_this_line(some_arg, some_arg, some_arg)
526         * a_very_specific_length(specific_length_arg)
527         * very_specific_length(Foo {
528             a: some_much_much_longer_value,
529         })
530         * some_value
531 }
532
533 fn issue_3003() {
534     let mut path: PathBuf = [
535         env!("CARGO_MANIFEST_DIR"),
536         "tests",
537         "support",
538         "dejavu-fonts-ttf-2.37",
539         "ttf",
540     ]
541     .iter()
542     .collect();
543 }
544
545 fn issue3226() {
546     {
547         {
548             {
549                 return Err(
550                     ErrorKind::ManagementInterfaceError("Server exited unexpectedly").into(),
551                 );
552             }
553         }
554     }
555     {
556         {
557             {
558                 break Err(
559                     ErrorKind::ManagementInterfaceError("Server exited unexpectedlyy").into(),
560                 );
561             }
562         }
563     }
564 }
565
566 // #3457
567 fn issue3457() {
568     {
569         {
570             {
571                 {
572                     {
573                         {
574                             {
575                                 {
576                                     {
577                                         {
578                                             {
579                                                 {
580                                                     {
581                                                         {
582                                                             {
583                                                                 {
584                                                                     {
585                                                                         {
586                                                                             {
587                                                                                 {
588                                                                                     {
589                                                                                         {
590                                                                                             {
591                                                                                                 {
592                                                                                                     println!("Test");
593                                                                                                 }
594                                                                                             }
595                                                                                         }
596                                                                                     }
597                                                                                 }
598                                                                             }
599                                                                         }
600                                                                     }
601                                                                 }
602                                                             }
603                                                         }
604                                                     }
605                                                 }
606                                             }
607                                         }
608                                     }
609                                 }
610                             }
611                         }
612                     }
613                 }
614             }
615         }
616     }
617 }
618
619 // #3498
620 static REPRO: &[usize] = &[
621     #[cfg(feature = "zero")]
622     0,
623 ];
624
625 fn overflow_with_attr() {
626     foo(
627         #[cfg(feature = "zero")]
628         0,
629     );
630     foobar(
631         #[cfg(feature = "zero")]
632         0,
633     );
634     foobar(
635         x,
636         y,
637         #[cfg(feature = "zero")]
638         {},
639     );
640 }