]> git.lizzy.rs Git - rust.git/blob - tests/source/expr.rs
Put multi-lined index on the next line if it fits in one line
[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     let test = if true { 5 } else { 3 };
49
50     if cond() {
51         something();
52     } else  if different_cond() {
53         something_else();
54     } else {
55         // Check subformatting
56         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
57     }
58 }
59
60 fn bar() {
61     let range = (   111111111 + 333333333333333333 + 1111 +   400000000000000000) .. (2222 +  2333333333333333);
62
63     let another_range = 5..some_func( a , b /* comment */);
64
65     for _  in  1 ..{ call_forever(); }
66
67     syntactically_correct(loop { sup( '?'); }, if cond { 0 } else { 1 });
68
69     let third = ..10;
70     let infi_range = ..   ;
71     let foo = 1..;
72     let bar = 5 ;
73     let nonsense = (10 .. 0)..(0..10);
74
75     loop{if true {break}}
76
77     let x = (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&
78              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
79              a);
80 }
81
82 fn baz() {
83     unsafe    /*    {}{}{}{{{{}}   */   {
84         let foo = 1u32;
85     }
86
87     unsafe /* very looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong comment */ {}
88
89     unsafe // So this is a very long comment.
90            // Multi-line, too.
91            // Will it still format correctly?
92     {
93     }
94
95     unsafe {
96         // Regular unsafe block
97     }
98
99     unsafe {
100         foo()
101     }
102
103     unsafe {
104         foo();
105     }
106 }
107
108 // Test some empty blocks.
109 fn qux() {
110     {}
111     // FIXME this one could be done better.
112     { /* a block with a comment */ }
113     {
114
115     }
116     {
117         // A block with a comment.
118     }
119 }
120
121 fn issue227() {
122     {
123         let handler = box DocumentProgressHandler::new(addr, DocumentProgressTask::DOMContentLoaded);
124     }
125 }
126
127 fn issue184(source: &str) {
128     for c in source.chars() {
129         if index < 'a' {
130             continue;
131         }
132     }
133 }
134
135 fn arrays() {
136     let x = [0,
137          1,
138          2,
139          3,
140          4,
141          5,
142          6,
143          7,
144          8,
145          9,
146          0,
147          1,
148          2,
149          3,
150          4,
151          5,
152          6,
153          7,
154          8,
155          9,
156          0,
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
172     let y = [/* comment */ 1, 2 /* post comment */, 3];
173
174     let xy =    [ strukt  { test123: value_one_two_three_four, turbo: coolio(), } , /* comment  */   1 ];
175
176         let a =WeightedChoice::new(&mut [Weighted {
177             weightweight: x,
178             item: 0,
179         },
180                                   Weighted {
181             weightweight: 1,
182             item: 1,
183         },
184                                   Weighted {
185             weightweight: x,
186             item: 2,
187         },
188                                   Weighted {
189             weightweight: 1,
190             item: 3,
191         }]);
192
193     let z = [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyyyyyyyyyyy, zzzzzzzzzzzzzzzzzz, q];
194
195     [ 1 +   3, 4 ,  5, 6, 7, 7, fncall::<Vec<_>>(3-1)]
196 }
197
198 fn returns() {
199     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && return;
200
201     return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
202 }
203
204 fn addrof() {
205     &    mut(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
206     &    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
207 }
208
209 fn casts() {
210     fn unpack(packed: u32) -> [u16; 2] {
211         [
212             (packed >> 16) as u16,
213             (packed >>  0) as u16,
214         ]
215     }
216
217     let some_trait_xxx = xxxxxxxxxxx + xxxxxxxxxxxxx
218         as SomeTraitXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX;
219     let slightly_longer_trait = yyyyyyyyy + yyyyyyyyyyy as SomeTraitYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY;
220 }
221
222 fn indices() {
223     let x = (aaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+cccccccccccccccc) [ x + y + z ];
224     let y = (aaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccc)[ xxxxx + yyyyy + zzzzz ];
225 }
226
227 fn repeats() {
228     let x = [aaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+cccccccccccccccc; x + y + z ];
229     let y = [aaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccc; xxxxx + yyyyy + zzzzz ];
230 }
231
232 fn blocks() {
233     if 1 + 1 == 2 {
234         println!("yay arithmetix!");
235     };
236 }
237
238 fn issue767() {
239     if false {
240         if false {
241         } else {
242             // A let binding here seems necessary to trigger it.
243             let _ = ();
244         }
245     } else if let false = false {
246     }
247 }
248
249 fn ranges() {
250     let x = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa .. bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
251     let y = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ... bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
252     let z = ... x ;
253
254     a ... b
255
256     // the expr below won't compile for some reason...
257     // let a = 0 ... ;
258 }
259
260 fn if_else() {
261   let exact = diff /
262             (if size == 0 {
263     1
264 } else {
265     size
266 });
267
268             let cx = tp1.x +
269                  any * radius *
270                  if anticlockwise {
271             1.0
272       } else {
273            -1.0
274        };
275 }
276
277 fn complex_if_else() {
278     if let Some(x) = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {
279     } else if let Some(x) = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {
280         ha();
281     } else if xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxx {
282         yo();
283     } else if let Some(x) = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {
284         ha();
285     } else if xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxxx {
286         yo();
287     }
288 }
289
290 fn issue1106() {
291     {
292     if let hir::ItemEnum(ref enum_def, ref generics) = self.ast_map.expect_item(enum_node_id).node {
293     }
294     }
295
296     for entry in
297     WalkDir::new(path)
298         .into_iter()
299         .filter_entry(|entry| exclusions.filter_entry(entry)) {
300     }
301 }
302
303 fn issue1570() {
304     a_very_long_function_name({some_func(1, {1})})
305 }
306
307 fn issue1714() {
308     v = &mut {v}[mid..];
309     let (left, right) = {v}.split_at_mut(mid);
310 }
311
312 // Multi-lined index should be put on the next line if it fits in one line.
313 fn issue1749() {
314     {
315         {
316             {
317                 if self.shape[(r as f32 + self.x_offset) as usize][(c as f32 + self.y_offset) as usize] != 0 {
318                     // hello
319                 }
320             }
321         }
322     }
323 }