]> git.lizzy.rs Git - rust.git/blob - tests/source/expr.rs
Avoid panic on macro inside deeply nested block
[rust.git] / tests / source / 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 = (a + first + simple + test + AAAAAAAAAAAAA + BBBBBBBBBBBBBBBBB + b + c);
11
12     let is_internalxxxx = self.source_map.span_to_filename(s) == self.source_map.span_to_filename(m.inner);
13
14     let some_val = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa * bbbb / (bbbbbb -
15         function_call(x, *very_long_pointer, y))
16     + 1000  ;
17
18 some_ridiculously_loooooooooooooooooooooong_function(10000 * 30000000000 + 40000 / 1002200000000
19                                                      - 50000 * sqrt(-1),
20                                                      trivial_value);
21     (((((((((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + a +
22              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaa)))))))))   ;
23
24     { for _ in 0..10 {} }
25
26     {{{{}}}}
27
28      if  1  + 2 > 0  { let result = 5; result } else { 4};
29
30     if  let   Some(x)  =  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa {
31         // Nothing
32     }
33
34     if  let   Some(x)  =  (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}
35
36     if let (some_very_large,
37             tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple) = 1
38         + 2 + 3 {
39     }
40
41     if let (some_very_large,
42             tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple) = 1111 + 2222 {}
43
44     if let (some_very_large, tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple) = 1
45  + 2 + 3 {
46     }
47
48     if let ast::ItemKind::Trait(_, unsafety, ref generics, ref type_param_bounds, ref trait_items) = item.node
49     {
50         // nothing
51     }
52
53     let test = if true { 5 } else { 3 };
54
55     if cond() {
56         something();
57     } else  if different_cond() {
58         something_else();
59     } else {
60         // Check subformatting
61         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
62     }
63
64     // #2884
65     let _ = [0; {struct Foo; impl Foo {const fn get(&self) -> usize {5}}; Foo.get()}];
66 }
67
68 fn bar() {
69     let range = (   111111111 + 333333333333333333 + 1111 +   400000000000000000) .. (2222 +  2333333333333333);
70
71     let another_range = 5..some_func( a , b /* comment */);
72
73     for _  in  1 ..{ call_forever(); }
74
75     syntactically_correct(loop { sup( '?'); }, if cond { 0 } else { 1 });
76
77     let third = ..10;
78     let infi_range = ..   ;
79     let foo = 1..;
80     let bar = 5 ;
81     let nonsense = (10 .. 0)..(0..10);
82
83     loop{if true {break}}
84
85     let x = (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&
86              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
87              a);
88 }
89
90 fn baz() {
91     unsafe    /*    {}{}{}{{{{}}   */   {
92         let foo = 1u32;
93     }
94
95     unsafe /* very looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong comment */ {}
96
97     unsafe // So this is a very long comment.
98            // Multi-line, too.
99            // Will it still format correctly?
100     {
101     }
102
103     unsafe {
104         // Regular unsafe block
105     }
106
107     unsafe {
108         foo()
109     }
110
111     unsafe {
112         foo();
113     }
114
115     // #2289
116     let identifier_0 = unsafe { this_is_58_chars_long_and_line_is_93_chars_long_xxxxxxxxxx };
117     let identifier_1 = unsafe { this_is_59_chars_long_and_line_is_94_chars_long_xxxxxxxxxxx };
118     let identifier_2 = unsafe { this_is_65_chars_long_and_line_is_100_chars_long_xxxxxxxxxxxxxxxx };
119     let identifier_3 = unsafe { this_is_66_chars_long_and_line_is_101_chars_long_xxxxxxxxxxxxxxxxx };
120 }
121
122 // Test some empty blocks.
123 fn qux() {
124     {}
125     // FIXME this one could be done better.
126     { /* a block with a comment */ }
127     {
128
129     }
130     {
131         // A block with a comment.
132     }
133 }
134
135 fn issue227() {
136     {
137         let handler = box DocumentProgressHandler::new(addr, DocumentProgressTask::DOMContentLoaded);
138     }
139 }
140
141 fn issue184(source: &str) {
142     for c in source.chars() {
143         if index < 'a' {
144             continue;
145         }
146     }
147 }
148
149 fn arrays() {
150     let x = [0,
151          1,
152          2,
153          3,
154          4,
155          5,
156          6,
157          7,
158          8,
159          9,
160          0,
161          1,
162          2,
163          3,
164          4,
165          5,
166          6,
167          7,
168          8,
169          9,
170          0,
171          7,
172          8,
173          9,
174          0,
175          1,
176          2,
177          3,
178          4,
179          5,
180          6,
181          7,
182          8,
183          9,
184          0];
185
186     let y = [/* comment */ 1, 2 /* post comment */, 3];
187
188     let xy =    [ strukt  { test123: value_one_two_three_four, turbo: coolio(), } , /* comment  */   1 ];
189
190         let a =WeightedChoice::new(&mut [Weighted {
191             weightweight: x,
192             item: 0,
193         },
194                                   Weighted {
195             weightweight: 1,
196             item: 1,
197         },
198                                   Weighted {
199             weightweight: x,
200             item: 2,
201         },
202                                   Weighted {
203             weightweight: 1,
204             item: 3,
205         }]);
206
207     let z = [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyyyyyyyyyyy, zzzzzzzzzzzzzzzzzz, q];
208
209     [ 1 +   3, 4 ,  5, 6, 7, 7, fncall::<Vec<_>>(3-1)]
210 }
211
212 fn returns() {
213     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && return;
214
215     return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
216 }
217
218 fn addrof() {
219     &    mut(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
220     &    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
221 }
222
223 fn casts() {
224     fn unpack(packed: u32) -> [u16; 2] {
225         [
226             (packed >> 16) as u16,
227             (packed >>  0) as u16,
228         ]
229     }
230
231     let some_trait_xxx = xxxxxxxxxxx + xxxxxxxxxxxxx
232         as SomeTraitXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX;
233     let slightly_longer_trait = yyyyyyyyy + yyyyyyyyyyy as SomeTraitYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY;
234 }
235
236 fn indices() {
237     let x = (aaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+cccccccccccccccc) [ x + y + z ];
238     let y = (aaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccc)[ xxxxx + yyyyy + zzzzz ];
239     let z = xxxxxxxxxx.x().y().zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz()[aaaaa];
240     let z = xxxxxxxxxx.x().y().zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz()[aaaaa];
241 }
242
243 fn repeats() {
244     let x = [aaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+cccccccccccccccc; x + y + z ];
245     let y = [aaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccc; xxxxx + yyyyy + zzzzz ];
246 }
247
248 fn blocks() {
249     if 1 + 1 == 2 {
250         println!("yay arithmetix!");
251     };
252 }
253
254 fn issue767() {
255     if false {
256         if false {
257         } else {
258             // A let binding here seems necessary to trigger it.
259             let _ = ();
260         }
261     } else if let false = false {
262     }
263 }
264
265 fn ranges() {
266     let x = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa .. bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
267     let y = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ..= bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
268     let z = ..= x ;
269
270     // #1766
271     let x = [0. ..10.0];
272     let x = [0. ..=10.0];
273
274     a ..= b
275
276     // the expr below won't compile because inclusive ranges need a defined end
277     // let a = 0 ..= ;
278 }
279
280 fn if_else() {
281   let exact = diff /
282             (if size == 0 {
283     1
284 } else {
285     size
286 });
287
288             let cx = tp1.x +
289                  any * radius *
290                  if anticlockwise {
291             1.0
292       } else {
293            -1.0
294        };
295 }
296
297 fn complex_if_else() {
298     if let Some(x) = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {
299     } else if let Some(x) = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {
300         ha();
301     } else if xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxx {
302         yo();
303     } else if let Some(x) = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {
304         ha();
305     } else if xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxxx {
306         yo();
307     }
308 }
309
310 fn issue1106() {
311     {
312     if let hir::ItemEnum(ref enum_def, ref generics) = self.ast_map.expect_item(enum_node_id).node {
313     }
314     }
315
316     for entry in
317     WalkDir::new(path)
318         .into_iter()
319         .filter_entry(|entry| exclusions.filter_entry(entry)) {
320     }
321 }
322
323 fn issue1570() {
324     a_very_long_function_name({some_func(1, {1})})
325 }
326
327 fn issue1714() {
328     v = &mut {v}[mid..];
329     let (left, right) = {v}.split_at_mut(mid);
330 }
331
332 // Multi-lined index should be put on the next line if it fits in one line.
333 fn issue1749() {
334     {
335         {
336             {
337                 if self.shape[(r as f32 + self.x_offset) as usize][(c as f32 + self.y_offset) as usize] != 0 {
338                     // hello
339                 }
340             }
341         }
342     }
343 }
344
345 // #1172
346 fn newlines_between_list_like_expr() {
347     foo(
348         xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
349
350         yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,
351
352         zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz,
353     );
354
355     vec![
356         xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
357
358         yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,
359
360         zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz,
361     ];
362
363     match x {
364         xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
365
366         yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy |
367
368         zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz => foo(a, b, c),
369         _ => bar(),
370     };
371 }
372
373 fn issue2178() {
374     Ok(result.iter().map(|item| ls_util::rls_to_location(item)).collect())
375 }
376
377 // #2493
378 impl Foo {
379 fn bar(&self) {
380     {
381         let x = match () {
382             () => {
383                 let i;
384                 i == self.install_config.storage.experimental_compressed_block_size as usize
385             }
386         };
387     }
388 }
389 }
390
391 fn dots() {
392     .. .. ..; // (.. (.. (..)))
393     ..= ..= ..;
394     (..) .. ..; // ((..) .. (..))
395 }
396
397 // #2676
398 // A function call with a large single argument.
399 fn foo() {
400     let my_var =
401         Mutex::new(RpcClientType::connect(server_iddd).chain_err(|| "Unable to create RPC client")?);
402 }
403
404 // #2704
405 // Method call with prefix and suffix.
406 fn issue2704() {
407     // We should not combine the callee with a multi-lined method call.
408     let requires = requires.set(&requires0
409                                 .concat(&requires1)
410                                 .concat(&requires2)
411                                 .distinct_total());
412     let requires = requires.set(box requires0
413                                 .concat(&requires1)
414                                 .concat(&requires2)
415                                 .distinct_total());
416     let requires = requires.set(requires0
417                                 .concat(&requires1)
418                                 .concat(&requires2)
419                                 .distinct_total() as u32);
420     let requires = requires.set(requires0
421                                 .concat(&requires1)
422                                 .concat(&requires2)
423                                 .distinct_total()?);
424     let requires = requires.set(!requires0
425                                 .concat(&requires1)
426                                 .concat(&requires2)
427                                 .distinct_total());
428     // We should combine a small callee with an argument.
429     bar(vec![22]
430         .into_iter()
431         .map(|x| x * 2)
432         .filter(|_| true)
433         .collect());
434     // But we should not combine a long callee with an argument.
435     barrrr(vec![22]
436         .into_iter()
437         .map(|x| x * 2)
438         .filter(|_| true)
439         .collect());
440 }
441
442 // #2782
443 fn issue2782() {
444     {let f={let f={{match f{F(f,_)=>{{loop{let f={match f{F(f,_)=>{{match f{F(f,_)=>{{loop{let f={let f={match f{'-'=>F(f,()),}};};}}}}}}}};}}}}}};};}
445 }
446
447 fn issue_2802() {
448     function_to_fill_this_line(some_arg, some_arg, some_arg)
449         * a_very_specific_length(specific_length_arg) * very_specific_length(Foo {
450         a: some_much_much_longer_value,
451     }) * some_value
452 }
453
454 fn issue_3003() {
455     let mut path: PathBuf = [
456         env!("CARGO_MANIFEST_DIR"),
457         "tests",
458         "support",
459         "dejavu-fonts-ttf-2.37",
460         "ttf",
461     ]
462         .iter()
463         .collect();
464 }
465
466 fn issue3226() {
467     {
468         {
469             {
470                 return Err(ErrorKind::ManagementInterfaceError("Server exited unexpectedly").into())
471             }
472         }
473     }
474     {
475         {
476             {
477                 break Err(ErrorKind::ManagementInterfaceError("Server exited unexpectedlyy").into())
478             }
479         }
480     }
481 }
482
483 // #3457
484 fn issue3457() {
485     {
486         {
487             {
488                 {
489                     {
490                         {
491                             {
492                                 {
493                                     {
494                                         {
495                                             {
496                                                 {
497                                                     {
498                                                         {
499                                                             {
500                                                                 {
501                                                                     {
502                                                                         {
503                                                                             {
504                                                                                 {
505                                                                                     {
506                                                                                         {
507                                                                                             {
508                                                                                                 {
509                                                                                                     println!("Test");
510                                                                                                 }
511                                                                                             }
512                                                                                         }
513                                                                                     }
514                                                                                 }
515                                                                             }
516                                                                         }
517                                                                     }
518                                                                 }
519                                                             }
520                                                         }
521                                                     }
522                                                 }
523                                             }
524                                         }
525                                     }
526                                 }
527                             }
528                         }
529                     }
530                 }
531             }
532         }
533     }
534 }