]> git.lizzy.rs Git - rust.git/blob - tests/source/expr.rs
Add a test for #2782
[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.codemap.span_to_filename(s) == self.codemap.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
65 fn bar() {
66     let range = (   111111111 + 333333333333333333 + 1111 +   400000000000000000) .. (2222 +  2333333333333333);
67
68     let another_range = 5..some_func( a , b /* comment */);
69
70     for _  in  1 ..{ call_forever(); }
71
72     syntactically_correct(loop { sup( '?'); }, if cond { 0 } else { 1 });
73
74     let third = ..10;
75     let infi_range = ..   ;
76     let foo = 1..;
77     let bar = 5 ;
78     let nonsense = (10 .. 0)..(0..10);
79
80     loop{if true {break}}
81
82     let x = (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&
83              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
84              a);
85 }
86
87 fn baz() {
88     unsafe    /*    {}{}{}{{{{}}   */   {
89         let foo = 1u32;
90     }
91
92     unsafe /* very looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong comment */ {}
93
94     unsafe // So this is a very long comment.
95            // Multi-line, too.
96            // Will it still format correctly?
97     {
98     }
99
100     unsafe {
101         // Regular unsafe block
102     }
103
104     unsafe {
105         foo()
106     }
107
108     unsafe {
109         foo();
110     }
111
112     // #2289
113     let identifier_0 = unsafe { this_is_58_chars_long_and_line_is_93_chars_long_xxxxxxxxxx };
114     let identifier_1 = unsafe { this_is_59_chars_long_and_line_is_94_chars_long_xxxxxxxxxxx };
115     let identifier_2 = unsafe { this_is_65_chars_long_and_line_is_100_chars_long_xxxxxxxxxxxxxxxx };
116     let identifier_3 = unsafe { this_is_66_chars_long_and_line_is_101_chars_long_xxxxxxxxxxxxxxxxx };
117 }
118
119 // Test some empty blocks.
120 fn qux() {
121     {}
122     // FIXME this one could be done better.
123     { /* a block with a comment */ }
124     {
125
126     }
127     {
128         // A block with a comment.
129     }
130 }
131
132 fn issue227() {
133     {
134         let handler = box DocumentProgressHandler::new(addr, DocumentProgressTask::DOMContentLoaded);
135     }
136 }
137
138 fn issue184(source: &str) {
139     for c in source.chars() {
140         if index < 'a' {
141             continue;
142         }
143     }
144 }
145
146 fn arrays() {
147     let x = [0,
148          1,
149          2,
150          3,
151          4,
152          5,
153          6,
154          7,
155          8,
156          9,
157          0,
158          1,
159          2,
160          3,
161          4,
162          5,
163          6,
164          7,
165          8,
166          9,
167          0,
168          7,
169          8,
170          9,
171          0,
172          1,
173          2,
174          3,
175          4,
176          5,
177          6,
178          7,
179          8,
180          9,
181          0];
182
183     let y = [/* comment */ 1, 2 /* post comment */, 3];
184
185     let xy =    [ strukt  { test123: value_one_two_three_four, turbo: coolio(), } , /* comment  */   1 ];
186
187         let a =WeightedChoice::new(&mut [Weighted {
188             weightweight: x,
189             item: 0,
190         },
191                                   Weighted {
192             weightweight: 1,
193             item: 1,
194         },
195                                   Weighted {
196             weightweight: x,
197             item: 2,
198         },
199                                   Weighted {
200             weightweight: 1,
201             item: 3,
202         }]);
203
204     let z = [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyyyyyyyyyyy, zzzzzzzzzzzzzzzzzz, q];
205
206     [ 1 +   3, 4 ,  5, 6, 7, 7, fncall::<Vec<_>>(3-1)]
207 }
208
209 fn returns() {
210     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && return;
211
212     return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
213 }
214
215 fn addrof() {
216     &    mut(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
217     &    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
218 }
219
220 fn casts() {
221     fn unpack(packed: u32) -> [u16; 2] {
222         [
223             (packed >> 16) as u16,
224             (packed >>  0) as u16,
225         ]
226     }
227
228     let some_trait_xxx = xxxxxxxxxxx + xxxxxxxxxxxxx
229         as SomeTraitXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX;
230     let slightly_longer_trait = yyyyyyyyy + yyyyyyyyyyy as SomeTraitYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY;
231 }
232
233 fn indices() {
234     let x = (aaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+cccccccccccccccc) [ x + y + z ];
235     let y = (aaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccc)[ xxxxx + yyyyy + zzzzz ];
236     let z = xxxxxxxxxx.x().y().zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz()[aaaaa];
237     let z = xxxxxxxxxx.x().y().zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz()[aaaaa];
238 }
239
240 fn repeats() {
241     let x = [aaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+cccccccccccccccc; x + y + z ];
242     let y = [aaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccc; xxxxx + yyyyy + zzzzz ];
243 }
244
245 fn blocks() {
246     if 1 + 1 == 2 {
247         println!("yay arithmetix!");
248     };
249 }
250
251 fn issue767() {
252     if false {
253         if false {
254         } else {
255             // A let binding here seems necessary to trigger it.
256             let _ = ();
257         }
258     } else if let false = false {
259     }
260 }
261
262 fn ranges() {
263     let x = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa .. bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
264     let y = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ..= bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
265     let z = ..= x ;
266
267     // #1766
268     let x = [0. ..10.0];
269     let x = [0. ..=10.0];
270
271     a ..= b
272
273     // the expr below won't compile because inclusive ranges need a defined end
274     // let a = 0 ..= ;
275 }
276
277 fn if_else() {
278   let exact = diff /
279             (if size == 0 {
280     1
281 } else {
282     size
283 });
284
285             let cx = tp1.x +
286                  any * radius *
287                  if anticlockwise {
288             1.0
289       } else {
290            -1.0
291        };
292 }
293
294 fn complex_if_else() {
295     if let Some(x) = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {
296     } else if let Some(x) = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {
297         ha();
298     } else if xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxx {
299         yo();
300     } else if let Some(x) = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {
301         ha();
302     } else if xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxxx {
303         yo();
304     }
305 }
306
307 fn issue1106() {
308     {
309     if let hir::ItemEnum(ref enum_def, ref generics) = self.ast_map.expect_item(enum_node_id).node {
310     }
311     }
312
313     for entry in
314     WalkDir::new(path)
315         .into_iter()
316         .filter_entry(|entry| exclusions.filter_entry(entry)) {
317     }
318 }
319
320 fn issue1570() {
321     a_very_long_function_name({some_func(1, {1})})
322 }
323
324 fn issue1714() {
325     v = &mut {v}[mid..];
326     let (left, right) = {v}.split_at_mut(mid);
327 }
328
329 // Multi-lined index should be put on the next line if it fits in one line.
330 fn issue1749() {
331     {
332         {
333             {
334                 if self.shape[(r as f32 + self.x_offset) as usize][(c as f32 + self.y_offset) as usize] != 0 {
335                     // hello
336                 }
337             }
338         }
339     }
340 }
341
342 // #1172
343 fn newlines_between_list_like_expr() {
344     foo(
345         xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
346
347         yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,
348
349         zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz,
350     );
351
352     vec![
353         xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
354
355         yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,
356
357         zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz,
358     ];
359
360     match x {
361         xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
362
363         yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy |
364
365         zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz => foo(a, b, c),
366         _ => bar(),
367     };
368 }
369
370 fn issue2178() {
371     Ok(result.iter().map(|item| ls_util::rls_to_location(item)).collect())
372 }
373
374 // #2493
375 impl Foo {
376 fn bar(&self) {
377     {
378         let x = match () {
379             () => {
380                 let i;
381                 i == self.install_config.storage.experimental_compressed_block_size as usize
382             }
383         };
384     }
385 }
386 }
387
388 fn dots() {
389     .. .. ..; // (.. (.. (..)))
390     ..= ..= ..;
391     (..) .. ..; // ((..) .. (..))
392 }
393
394 // #2676
395 // A function call with a large single argument.
396 fn foo() {
397     let my_var =
398         Mutex::new(RpcClientType::connect(server_iddd).chain_err(|| "Unable to create RPC client")?);
399 }
400
401 // #2704
402 // Method call with prefix and suffix.
403 fn issue2704() {
404     // We should not combine the callee with a multi-lined method call.
405     let requires = requires.set(&requires0
406                                 .concat(&requires1)
407                                 .concat(&requires2)
408                                 .distinct_total());
409     let requires = requires.set(box requires0
410                                 .concat(&requires1)
411                                 .concat(&requires2)
412                                 .distinct_total());
413     let requires = requires.set(requires0
414                                 .concat(&requires1)
415                                 .concat(&requires2)
416                                 .distinct_total() as u32);
417     let requires = requires.set(requires0
418                                 .concat(&requires1)
419                                 .concat(&requires2)
420                                 .distinct_total()?);
421     let requires = requires.set(!requires0
422                                 .concat(&requires1)
423                                 .concat(&requires2)
424                                 .distinct_total());
425     // We should combine a small callee with an argument.
426     bar(vec![22]
427         .into_iter()
428         .map(|x| x * 2)
429         .filter(|_| true)
430         .collect());
431     // But we should not combine a long callee with an argument.
432     barrrr(vec![22]
433         .into_iter()
434         .map(|x| x * 2)
435         .filter(|_| true)
436         .collect());
437 }
438
439 // #2782
440 fn issue2782() {
441     {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,()),}};};}}}}}}}};}}}}}};};}
442 }