]> git.proxmox.com Git - rustc.git/blame - src/tools/rustfmt/tests/source/expr.rs
New upstream version 1.52.1+dfsg1
[rustc.git] / src / tools / rustfmt / tests / source / expr.rs
CommitLineData
f20569fa
XL
1// rustfmt-normalize_comments: true
2// rustfmt-wrap_comments: true
3// Test expressions
4
5fn 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
18some_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
68fn 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
90fn 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.
123fn 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
135fn issue227() {
136 {
137 let handler = box DocumentProgressHandler::new(addr, DocumentProgressTask::DOMContentLoaded);
138 }
139}
140
141fn issue184(source: &str) {
142 for c in source.chars() {
143 if index < 'a' {
144 continue;
145 }
146 }
147}
148
149fn 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
212fn returns() {
213 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && return;
214
215 return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
216}
217
218fn addrof() {
219 & mut(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
220 & (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
221
222 // raw reference operator
223 & raw const a;
224 & raw mut b;
225}
226
227fn casts() {
228 fn unpack(packed: u32) -> [u16; 2] {
229 [
230 (packed >> 16) as u16,
231 (packed >> 0) as u16,
232 ]
233 }
234
235 let some_trait_xxx = xxxxxxxxxxx + xxxxxxxxxxxxx
236 as SomeTraitXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX;
237 let slightly_longer_trait = yyyyyyyyy + yyyyyyyyyyy as SomeTraitYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY;
238}
239
240fn indices() {
241 let x = (aaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+cccccccccccccccc) [ x + y + z ];
242 let y = (aaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccc)[ xxxxx + yyyyy + zzzzz ];
243 let z = xxxxxxxxxx.x().y().zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz()[aaaaa];
244 let z = xxxxxxxxxx.x().y().zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz()[aaaaa];
245}
246
247fn repeats() {
248 let x = [aaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+cccccccccccccccc; x + y + z ];
249 let y = [aaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccc; xxxxx + yyyyy + zzzzz ];
250}
251
252fn blocks() {
253 if 1 + 1 == 2 {
254 println!("yay arithmetix!");
255 };
256}
257
258fn issue767() {
259 if false {
260 if false {
261 } else {
262 // A let binding here seems necessary to trigger it.
263 let _ = ();
264 }
265 } else if let false = false {
266 }
267}
268
269fn ranges() {
270 let x = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa .. bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
271 let y = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ..= bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
272 let z = ..= x ;
273
274 // #1766
275 let x = [0. ..10.0];
276 let x = [0. ..=10.0];
277
278 a ..= b
279
280 // the expr below won't compile because inclusive ranges need a defined end
281 // let a = 0 ..= ;
282}
283
284fn if_else() {
285 let exact = diff /
286 (if size == 0 {
287 1
288} else {
289 size
290});
291
292 let cx = tp1.x +
293 any * radius *
294 if anticlockwise {
295 1.0
296 } else {
297 -1.0
298 };
299}
300
301fn complex_if_else() {
302 if let Some(x) = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {
303 } else if let Some(x) = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {
304 ha();
305 } else if xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxx {
306 yo();
307 } else if let Some(x) = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {
308 ha();
309 } else if xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxxx {
310 yo();
311 }
312}
313
314fn issue1106() {
315 {
316 if let hir::ItemEnum(ref enum_def, ref generics) = self.ast_map.expect_item(enum_node_id).node {
317 }
318 }
319
320 for entry in
321 WalkDir::new(path)
322 .into_iter()
323 .filter_entry(|entry| exclusions.filter_entry(entry)) {
324 }
325}
326
327fn issue1570() {
328 a_very_long_function_name({some_func(1, {1})})
329}
330
331fn issue1714() {
332 v = &mut {v}[mid..];
333 let (left, right) = {v}.split_at_mut(mid);
334}
335
336// Multi-lined index should be put on the next line if it fits in one line.
337fn issue1749() {
338 {
339 {
340 {
341 if self.shape[(r as f32 + self.x_offset) as usize][(c as f32 + self.y_offset) as usize] != 0 {
342 // hello
343 }
344 }
345 }
346 }
347}
348
349// #1172
350fn newlines_between_list_like_expr() {
351 foo(
352 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
353
354 yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,
355
356 zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz,
357 );
358
359 vec![
360 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
361
362 yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,
363
364 zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz,
365 ];
366
367 match x {
368 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
369
370 yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy |
371
372 zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz => foo(a, b, c),
373 _ => bar(),
374 };
375}
376
377fn issue2178() {
378 Ok(result.iter().map(|item| ls_util::rls_to_location(item)).collect())
379}
380
381// #2493
382impl Foo {
383fn bar(&self) {
384 {
385 let x = match () {
386 () => {
387 let i;
388 i == self.install_config.storage.experimental_compressed_block_size as usize
389 }
390 };
391 }
392}
393}
394
395fn dots() {
396 .. .. ..; // (.. (.. (..)))
397 ..= ..= ..;
398 (..) .. ..; // ((..) .. (..))
399}
400
401// #2676
402// A function call with a large single argument.
403fn foo() {
404 let my_var =
405 Mutex::new(RpcClientType::connect(server_iddd).chain_err(|| "Unable to create RPC client")?);
406}
407
408// #2704
409// Method call with prefix and suffix.
410fn issue2704() {
411 // We should not combine the callee with a multi-lined method call.
412 let requires = requires.set(&requires0
413 .concat(&requires1)
414 .concat(&requires2)
415 .distinct_total());
416 let requires = requires.set(box requires0
417 .concat(&requires1)
418 .concat(&requires2)
419 .distinct_total());
420 let requires = requires.set(requires0
421 .concat(&requires1)
422 .concat(&requires2)
423 .distinct_total() as u32);
424 let requires = requires.set(requires0
425 .concat(&requires1)
426 .concat(&requires2)
427 .distinct_total()?);
428 let requires = requires.set(!requires0
429 .concat(&requires1)
430 .concat(&requires2)
431 .distinct_total());
432 // We should combine a small callee with an argument.
433 bar(vec![22]
434 .into_iter()
435 .map(|x| x * 2)
436 .filter(|_| true)
437 .collect());
438 // But we should not combine a long callee with an argument.
439 barrrr(vec![22]
440 .into_iter()
441 .map(|x| x * 2)
442 .filter(|_| true)
443 .collect());
444}
445
446// #2782
447fn issue2782() {
448 {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,()),}};};}}}}}}}};}}}}}};};}
449}
450
451fn issue_2802() {
452 function_to_fill_this_line(some_arg, some_arg, some_arg)
453 * a_very_specific_length(specific_length_arg) * very_specific_length(Foo {
454 a: some_much_much_longer_value,
455 }) * some_value
456}
457
458fn issue_3003() {
459 let mut path: PathBuf = [
460 env!("CARGO_MANIFEST_DIR"),
461 "tests",
462 "support",
463 "dejavu-fonts-ttf-2.37",
464 "ttf",
465 ]
466 .iter()
467 .collect();
468}
469
470fn issue3226() {
471 {
472 {
473 {
474 return Err(ErrorKind::ManagementInterfaceError("Server exited unexpectedly").into())
475 }
476 }
477 }
478 {
479 {
480 {
481 break Err(ErrorKind::ManagementInterfaceError("Server exited unexpectedlyy").into())
482 }
483 }
484 }
485}
486
487// #3457
488fn issue3457() {
489 {
490 {
491 {
492 {
493 {
494 {
495 {
496 {
497 {
498 {
499 {
500 {
501 {
502 {
503 {
504 {
505 {
506 {
507 {
508 {
509 {
510 {
511 {
512 {
513 println!("Test");
514 }
515 }
516 }
517 }
518 }
519 }
520 }
521 }
522 }
523 }
524 }
525 }
526 }
527 }
528 }
529 }
530 }
531 }
532 }
533 }
534 }
535 }
536 }
537 }
538}
539
540// #3498
541static REPRO: &[usize] = &[#[cfg(feature = "zero")]
542 0];
543
544fn overflow_with_attr() {
545 foo(#[cfg(feature = "zero")]
546 0);
547 foobar(#[cfg(feature = "zero")]
548 0);
549 foobar(x, y, #[cfg(feature = "zero")]
550 {});
551}
552
553
554// https://github.com/rust-lang/rustfmt/issues/3765
555fn foo() {
556 async {
557 // Do
558 // some
559 // work
560 }
561 .await;
562
563 async {
564 // Do
565 // some
566 // work
567 }
568 .await;
569}
570
571fn underscore() {
572 _= 1;
573 _;
574 [ _,a,_ ] = [1, 2, 3];
575 (a, _) = (8, 9);
576 TupleStruct( _, a) = TupleStruct(2, 2);
577
578 let _ : usize = foo(_, _);
579}