]> git.lizzy.rs Git - rust.git/blob - src/tools/rustfmt/tests/source/expr-block.rs
Rollup merge of #107166 - petrochenkov:nooptable, r=oli-obk
[rust.git] / src / tools / rustfmt / tests / source / expr-block.rs
1 // Test expressions with block formatting.
2
3 fn arrays() {
4     [      ];
5     let empty = [];
6
7     let foo = [a_long_name, a_very_lng_name, a_long_name];
8
9     let foo = [a_long_name, a_very_lng_name, a_long_name, a_very_lng_name, a_long_name, a_very_lng_name, a_long_name, a_very_lng_name];
10
11     vec![a_long_name, a_very_lng_name, a_long_name, a_very_lng_name, a_long_name, a_very_lng_name, a_very_lng_name];
12
13     [a_long_name, a_very_lng_name, a_long_name, a_very_lng_name, a_long_name, a_very_lng_name, a_very_lng_name]
14 }
15
16 fn arrays() {
17     let x = [0,
18          1,
19          2,
20          3,
21          4,
22          5,
23          6,
24          7,
25          8,
26          9,
27          0,
28          1,
29          2,
30          3,
31          4,
32          5,
33          6,
34          7,
35          8,
36          9,
37          0,
38          7,
39          8,
40          9,
41          0,
42          1,
43          2,
44          3,
45          4,
46          5,
47          6,
48          7,
49          8,
50          9,
51          0];
52
53     let y = [/* comment */ 1, 2 /* post comment */, 3];
54
55     let xy =    [ strukt  { test123: value_one_two_three_four, turbo: coolio(), } , /* comment  */   1 ];
56
57         let a =WeightedChoice::new(&mut [Weighted {
58             weight: x,
59             item: 0,
60         },
61                                   Weighted {
62             weight: 1,
63             item: 1,
64         },
65                                   Weighted {
66             weight: x,
67             item: 2,
68         },
69                                   Weighted {
70             weight: 1,
71             item: 3,
72         }]);
73
74     let z = [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyyyyyyyyyyy, zzzzzzzzzzzzzzzzz, q];
75
76     [ 1 +   3, 4 ,  5, 6, 7, 7, fncall::<Vec<_>>(3-1)]
77 }
78
79 fn function_calls() {
80     let items = itemize_list(context.source_map,
81                              args.iter(),
82                              ")",
83                              |item| item.span.lo(),
84                              |item| item.span.hi(),
85                              |item| {
86                                  item.rewrite(context,
87                                               Shape {
88                                                   width: remaining_width,
89                                                   ..nested_shape
90                                               })
91                              },
92                              span.lo(),
93                              span.hi());
94
95     itemize_list(context.source_map,
96                              args.iter(),
97                              ")",
98                              |item| item.span.lo(),
99                              |item| item.span.hi(),
100                              |item| {
101                                  item.rewrite(context,
102                                               Shape {
103                                                   width: remaining_width,
104                                                   ..nested_shape
105                                               })
106                              },
107                              span.lo(),
108                              span.hi())
109 }
110
111 fn macros() {
112     baz!(do_not, add, trailing, commas, inside, of, function, like, macros, even, if_they, are, long);
113
114     baz!(one_item_macro_which_is_also_loooooooooooooooooooooooooooooooooooooooooooooooong);
115
116     let _ = match option {
117         None => baz!(function, like, macro_as, expression, which, is, loooooooooooooooong),
118         Some(p) => baz!(one_item_macro_as_expression_which_is_also_loooooooooooooooong),
119     };
120 }
121
122 fn issue_1450() {
123     if selfstate
124         .compare_exchandsfasdsdfgsdgsdfgsdfgsdfgsdfgsdfgfsfdsage_weak(
125             STATE_PARKED,
126             STATE_UNPARKED,
127             Release,
128             Relaxed,
129             Release,
130             Relaxed,
131         )
132         .is_ok() {
133         return;
134     }
135 }
136
137 fn foo() {
138     if real_total <= limit && !pre_line_comments &&
139             !items.into_iter().any(|item| item.as_ref().is_multiline()) {
140          DefinitiveListTactic::Horizontal
141     }
142 }
143
144 fn combine_block() {
145     foo(
146         Bar {
147             x: value,
148             y: value2,
149         },
150     );
151
152     foo((Bar {
153         x: value,
154         y: value2,
155     },));
156
157     foo((1, 2, 3, Bar {
158         x: value,
159         y: value2,
160     }));
161
162     foo((1, 2, 3, |x| {
163         let y = x + 1;
164         let z = y + 1;
165         z
166     }));
167
168     let opt = Some(
169         Struct(
170             long_argument_one,
171             long_argument_two,
172             long_argggggggg,
173         ),
174     );
175
176     do_thing(
177         |param| {
178             action();
179             foo(param)
180         },
181     );
182
183     do_thing(
184         x,
185         |param| {
186             action();
187             foo(param)
188         },
189     );
190
191     do_thing(
192         x,
193         (
194             1,
195             2,
196             3,
197             |param| {
198                 action();
199                 foo(param)
200             },
201         ),
202     );
203
204     Ok(
205         some_function(
206             lllllllllong_argument_one,
207             lllllllllong_argument_two,
208             lllllllllllllllllllllllllllllong_argument_three,
209         ),
210     );
211
212     foo(
213         thing,
214         bar(
215             param2,
216             pparam1param1param1param1param1param1param1param1param1param1aram1,
217             param3,
218         ),
219     );
220
221     foo.map_or(
222         || {
223             Ok(
224                 SomeStruct {
225                     f1: 0,
226                     f2: 0,
227                     f3: 0,
228                 },
229             )
230         },
231     );
232
233     match opt {
234         Some(x) => somefunc(anotherfunc(
235             long_argument_one,
236             long_argument_two,
237             long_argument_three,
238         )),
239         Some(x) => |x| {
240             let y = x + 1;
241             let z = y + 1;
242             z
243         },
244         Some(x) => (1, 2, |x| {
245             let y = x + 1;
246             let z = y + 1;
247             z
248         }),
249         Some(x) => SomeStruct {
250             f1: long_argument_one,
251             f2: long_argument_two,
252             f3: long_argument_three,
253         },
254         None => Ok(SomeStruct {
255             f1: long_argument_one,
256             f2: long_argument_two,
257             f3: long_argument_three,
258         }),
259     };
260
261     match x {
262         y => func(
263             xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
264         ),
265         _ => func(
266             x,
267             yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,
268             zzz,
269         ),
270     }
271 }
272
273 fn issue_1862() {
274     foo(
275         /* bar = */ None ,
276         something_something,
277         /* baz = */ None ,
278         /* This comment waaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaay too long to be kept on the same line */ None ,
279         /* com */ this_last_arg_is_tooooooooooooooooooooooooooooooooo_long_to_be_kept_with_the_pre_comment ,
280     )
281 }
282
283 fn issue_3025() {
284     foo(
285         // This describes the argument below.
286         /* bar = */ None ,
287         // This describes the argument below.
288         something_something,
289         // This describes the argument below. */
290         None ,
291         // This describes the argument below.
292         /* This comment waaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaay too long to be kept on the same line */ None ,
293         // This describes the argument below.
294         /* com */ this_last_arg_is_tooooooooooooooooooooooooooooooooo_long_to_be_kept_with_the_pre_comment ,
295     )
296 }
297
298 fn issue_1878() {
299     let channel: &str = seq.next_element()?.ok_or_else(|| de::Error::invalid_length(2, &self))?;
300 }