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