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