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