]> git.lizzy.rs Git - rust.git/blob - tests/target/match.rs
Merge pull request #2101 from topecongiro/issue-2099
[rust.git] / tests / target / match.rs
1 // rustfmt-normalize_comments: true
2 // Match expressions.
3
4 fn foo() {
5     // A match expression.
6     match x {
7         // Some comment.
8         a => foo(),
9         b if 0 < 42 => foo(),
10         c => {
11             // Another comment.
12             // Comment.
13             an_expression;
14             foo()
15         }
16         Foo(ref bar) => {
17             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
18         }
19         Pattern1 | Pattern2 | Pattern3 => false,
20         Paternnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn |
21         Paternnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn => blah,
22         Patternnnnnnnnnnnnnnnnnnn |
23         Patternnnnnnnnnnnnnnnnnnn |
24         Patternnnnnnnnnnnnnnnnnnn |
25         Patternnnnnnnnnnnnnnnnnnn => meh,
26
27         Patternnnnnnnnnnnnnnnnnnn | Patternnnnnnnnnnnnnnnnnnn if looooooooooooooooooong_guard => {
28             meh
29         }
30
31         Patternnnnnnnnnnnnnnnnnnnnnnnnn | Patternnnnnnnnnnnnnnnnnnnnnnnnn
32             if looooooooooooooooooooooooooooooooooooooooong_guard =>
33         {
34             meh
35         }
36
37         // Test that earlier patterns can take the guard space
38         (aaaa, bbbbb, ccccccc, aaaaa, bbbbbbbb, cccccc, aaaa, bbbbbbbb, cccccc, dddddd) |
39         Patternnnnnnnnnnnnnnnnnnnnnnnnn if loooooooooooooooooooooooooooooooooooooooooong_guard => {}
40
41         _ => {}
42         ast::PathParameters::AngleBracketedParameters(ref data)
43             if data.lifetimes.len() > 0 || data.types.len() > 0 || data.bindings.len() > 0 => {}
44     }
45
46     let whatever = match something {
47         /// DOC COMMENT!
48         Some(_) => 42,
49         // Comment on an attribute.
50         #[an_attribute]
51         // Comment after an attribute.
52         None => 0,
53         #[rustfmt_skip]
54         Blurb     =>     {                  }
55     };
56 }
57
58 // Test that a match on an overflow line is laid out properly.
59 fn main() {
60     let sub_span =
61         match xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {
62             Some(sub_span) => Some(sub_span),
63             None => sub_span,
64         };
65 }
66
67 // Test that one-line bodies align.
68 fn main() {
69     match r {
70         Variableeeeeeeeeeeeeeeeee => (
71             "variable",
72             vec!["id", "name", "qualname", "value", "type", "scopeid"],
73             true,
74             true,
75         ),
76         Enummmmmmmmmmmmmmmmmmmmm => (
77             "enum",
78             vec!["id", "qualname", "scopeid", "value"],
79             true,
80             true,
81         ),
82         Variantttttttttttttttttttttttt => (
83             "variant",
84             vec!["id", "name", "qualname", "type", "value", "scopeid"],
85             true,
86             true,
87         ),
88     };
89
90     match x {
91         y => { /*Block with comment. Preserve me.*/ }
92         z => {
93             stmt();
94         }
95     }
96 }
97
98 fn matches() {
99     match 1 {
100         -1 => 10,
101         1 => 1, // foo
102         2 => 2,
103         // bar
104         3 => 3,
105         _ => 0, // baz
106     }
107 }
108
109 fn match_skip() {
110     let _ = match Some(1) {
111         #[rustfmt_skip]
112         Some( n ) => n,
113         None => 1,
114     };
115 }
116
117 fn issue339() {
118     match a {
119         b => {}
120         c => {}
121         d => {}
122         e => {}
123         // collapsing here is safe
124         ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff => {}
125         // collapsing here exceeds line length
126         ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffg => {
127         }
128         h => {
129             // comment above block
130         }
131         i => {}
132         // comment below block
133         j => {
134             // comment inside block
135         }
136         j2 => {
137             // comments inside...
138         }
139         // ... and after
140         // TODO uncomment when vertical whitespace is handled better
141         // k => {
142         //
143         //     // comment with WS above
144         // }
145         // l => {
146         //     // comment with ws below
147         //
148         // }
149         m => {}
150         n => {}
151         o => {}
152         p => {
153             // Dont collapse me
154         }
155         q => {}
156         r => {}
157         s => 0, // s comment
158         // t comment
159         t => 1,
160         u => 2,
161         v => {} /* funky block
162                  * comment */
163                 /* final comment */
164     }
165 }
166
167 fn issue355() {
168     match mac {
169         a => println!("a", b),
170         b => vec![1, 2],
171         c => vec![3; 4],
172         d => println!("a", b),
173         e => vec![1, 2],
174         f => vec![3; 4],
175         h => println!("a", b), // h comment
176         i => vec![1, 2],       // i comment
177         j => vec![3; 4],       // j comment
178         // k comment
179         k => println!("a", b),
180         // l comment
181         l => vec![1, 2],
182         // m comment
183         m => vec![3; 4],
184         // Rewrite splits macro
185         nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn => {
186             println!("a", b)
187         }
188         // Rewrite splits macro
189         oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo => {
190             vec![1, 2]
191         }
192         // Macro support fails to recognise this macro as splitable
193         // We push the whole expr to a new line, TODO split this macro as well
194         pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp => {
195             vec![3; 4]
196         }
197         // q, r and s: Rewrite splits match arm
198         qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq => {
199             println!("a", b)
200         }
201         rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr => {
202             vec![1, 2]
203         }
204         ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss => {
205             vec![3; 4]
206         }
207         // Funky bracketing styles
208         t => println!{"a", b},
209         u => vec![1, 2],
210         v => vec![3; 4],
211         w => println!["a", b],
212         x => vec![1, 2],
213         y => vec![3; 4],
214         // Brackets with comments
215         tc => println!{"a", b}, // comment
216         uc => vec![1, 2],       // comment
217         vc => vec![3; 4],       // comment
218         wc => println!["a", b], // comment
219         xc => vec![1, 2],       // comment
220         yc => vec![3; 4],       // comment
221         yd => looooooooooooooooooooooooooooooooooooooooooooooooooooooooong_func(
222             aaaaaaaaaa,
223             bbbbbbbbbb,
224             cccccccccc,
225             dddddddddd,
226         ),
227     }
228 }
229
230 fn issue280() {
231     {
232         match x {
233             CompressionMode::DiscardNewline | CompressionMode::CompressWhitespaceNewline => {
234                 ch == '\n'
235             }
236             ast::ItemConst(ref typ, ref expr) => {
237                 self.process_static_or_const_item(item, &typ, &expr)
238             }
239         }
240     }
241 }
242
243 fn issue383() {
244     match resolution.last_private {
245         LastImport { .. } => false,
246         _ => true,
247     };
248 }
249
250 fn issue507() {
251     match 1 {
252         1 => unsafe { std::intrinsics::abort() },
253         _ => (),
254     }
255 }
256
257 fn issue508() {
258     match s.type_id() {
259         Some(
260             NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLCanvasElement)),
261         ) => true,
262         Some(
263             NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLObjectElement)),
264         ) => s.has_object_data(),
265         Some(NodeTypeId::Element(_)) => false,
266     }
267 }
268
269 fn issue496() {
270     {
271         {
272             {
273                 match def {
274                     def::DefConst(def_id) | def::DefAssociatedConst(def_id) => {
275                         match const_eval::lookup_const_by_id(cx.tcx, def_id, Some(self.pat.id)) {
276                             Some(const_expr) => x,
277                         }
278                     }
279                 }
280             }
281         }
282     }
283 }
284
285 fn issue494() {
286     {
287         match stmt.node {
288             hir::StmtExpr(ref expr, id) | hir::StmtSemi(ref expr, id) => {
289                 result.push(StmtRef::Mirror(Box::new(Stmt {
290                     span: stmt.span,
291                     kind: StmtKind::Expr {
292                         scope: cx.tcx.region_maps.node_extent(id),
293                         expr: expr.to_ref(),
294                     },
295                 })))
296             }
297         }
298     }
299 }
300
301 fn issue386() {
302     match foo {
303         BiEq | BiLt | BiLe | BiNe | BiGt | BiGe => true,
304         BiAnd |
305         BiOr |
306         BiAdd |
307         BiSub |
308         BiMul |
309         BiDiv |
310         BiRem |
311         BiBitXor |
312         BiBitAnd |
313         BiBitOr |
314         BiShl |
315         BiShr => false,
316     }
317 }
318
319 fn guards() {
320     match foo {
321         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
322             if foooooooooooooo && barrrrrrrrrrrr => {}
323         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
324         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
325             if foooooooooooooo && barrrrrrrrrrrr => {}
326         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
327             if fooooooooooooooooooooo
328                 && (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
329                     || cccccccccccccccccccccccccccccccccccccccc) => {}
330     }
331 }
332
333 fn issue1371() {
334     Some(match type_ {
335         sfEvtClosed => Closed,
336         sfEvtResized => {
337             let e = unsafe { *event.size.as_ref() };
338
339             Resized {
340                 width: e.width,
341                 height: e.height,
342             }
343         }
344         sfEvtLostFocus => LostFocus,
345         sfEvtGainedFocus => GainedFocus,
346         sfEvtTextEntered => TextEntered {
347             unicode: unsafe {
348                 ::std::char::from_u32((*event.text.as_ref()).unicode)
349                     .expect("Invalid unicode encountered on TextEntered event")
350             },
351         },
352         sfEvtKeyPressed => {
353             let e = unsafe { event.key.as_ref() };
354
355             KeyPressed {
356                 code: unsafe { ::std::mem::transmute(e.code) },
357                 alt: e.alt.to_bool(),
358                 ctrl: e.control.to_bool(),
359                 shift: e.shift.to_bool(),
360                 system: e.system.to_bool(),
361             }
362         }
363         sfEvtKeyReleased => {
364             let e = unsafe { event.key.as_ref() };
365
366             KeyReleased {
367                 code: unsafe { ::std::mem::transmute(e.code) },
368                 alt: e.alt.to_bool(),
369                 ctrl: e.control.to_bool(),
370                 shift: e.shift.to_bool(),
371                 system: e.system.to_bool(),
372             }
373         }
374     })
375 }
376
377 fn issue1395() {
378     let bar = Some(true);
379     let foo = Some(true);
380     let mut x = false;
381     bar.and_then(|_| match foo {
382         None => None,
383         Some(b) => {
384             x = true;
385             Some(b)
386         }
387     });
388 }
389
390 fn issue1456() {
391     Ok(Recording {
392         artists: match reader.evaluate(".//mb:recording/mb:artist-credit/mb:name-credit")? {
393             Nodeset(nodeset) => {
394                 let res: Result<Vec<ArtistRef>, ReadError> = nodeset
395                     .iter()
396                     .map(|node| {
397                         XPathNodeReader::new(node, &context).and_then(|r| ArtistRef::from_xml(&r))
398                     })
399                     .collect();
400                 res?
401             }
402             _ => Vec::new(),
403         },
404     })
405 }
406
407 fn issue1460() {
408     let _ = match foo {
409         REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT => {
410             "internal_spec_insert_internal_spec_insert_internal_spec_insert"
411         }
412         _ => "reorder_something",
413     };
414 }
415
416 fn issue525() {
417     foobar(
418         f,
419         "{}",
420         match *self {
421             TaskState::Started => "started",
422             TaskState::Success => "success",
423             TaskState::Failed => "failed",
424         },
425     );
426 }
427
428 // #1838, #1839
429 fn match_with_near_max_width() {
430     let (this_line_uses_99_characters_and_is_formatted_properly, x012345) = match some_expression {
431         _ => unimplemented!(),
432     };
433
434     let (should_be_formatted_like_the_line_above_using_100_characters, x0) = match some_expression {
435         _ => unimplemented!(),
436     };
437
438     let (should_put_the_brace_on_the_next_line_using_101_characters, x0000) = match some_expression
439     {
440         _ => unimplemented!(),
441     };
442     match m {
443         Variant::Tag |
444         Variant::Tag2 |
445         Variant::Tag3 |
446         Variant::Tag4 |
447         Variant::Tag5 |
448         Variant::Tag6 => {}
449     }
450 }
451
452 fn match_with_trailing_spaces() {
453     match x {
454         #![allow(simple_match)]
455         Some(..) => 0,
456         None => 1,
457     }
458 }
459
460 fn issue_2099() {
461     let a = match x {};
462     let b = match x {};
463
464     match x {}
465 }