]> git.lizzy.rs Git - rust.git/blob - tests/target/match.rs
Merge pull request #912 from rust-lang-nursery/pat-simple-mixed
[rust.git] / tests / target / match.rs
1 // Match expressions.
2
3 fn foo() {
4     // A match expression.
5     match x {
6         // Some comment.
7         a => foo(),
8         b if 0 < 42 => foo(),
9         c => {
10             // Another comment.
11             // Comment.
12             an_expression;
13             foo()
14         }
15         Foo(ref bar) => {
16             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
17         }
18         Pattern1 | Pattern2 | Pattern3 => false,
19         Paternnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn |
20         Paternnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn => blah,
21         Patternnnnnnnnnnnnnnnnnnn |
22         Patternnnnnnnnnnnnnnnnnnn |
23         Patternnnnnnnnnnnnnnnnnnn |
24         Patternnnnnnnnnnnnnnnnnnn => meh,
25
26         Patternnnnnnnnnnnnnnnnnnn |
27         Patternnnnnnnnnnnnnnnnnnn if looooooooooooooooooong_guard => meh,
28
29         Patternnnnnnnnnnnnnnnnnnnnnnnnn |
30         Patternnnnnnnnnnnnnnnnnnnnnnnnn if looooooooooooooooooooooooooooooooooooooooong_guard => {
31             meh
32         }
33
34         // Test that earlier patterns can take the guard space
35         (aaaa, bbbbb, ccccccc, aaaaa, bbbbbbbb, cccccc, aaaa, bbbbbbbb, cccccc, dddddd) |
36         Patternnnnnnnnnnnnnnnnnnnnnnnnn if loooooooooooooooooooooooooooooooooooooooooong_guard => {}
37
38         _ => {}
39         ast::PathParameters::AngleBracketedParameters(ref data) if data.lifetimes.len() > 0 ||
40                                                                    data.types.len() > 0 ||
41                                                                    data.bindings.len() > 0 => {}
42     }
43
44     let whatever = match something {
45         /// DOC COMMENT!
46         Some(_) => 42,
47         // Comment on an attribute.
48         #[an_attribute]
49         // Comment after an attribute.
50         None => 0,
51         #[rustfmt_skip]
52         Blurb     =>     {                  }
53     };
54 }
55
56 // Test that a match on an overflow line is laid out properly.
57 fn main() {
58     let sub_span =
59         match xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {
60             Some(sub_span) => Some(sub_span),
61             None => sub_span,
62         };
63 }
64
65 // Test that one-line bodies align.
66 fn main() {
67     match r {
68         Variableeeeeeeeeeeeeeeeee => {
69             ("variable", vec!["id", "name", "qualname", "value", "type", "scopeid"], true, true)
70         }
71         Enummmmmmmmmmmmmmmmmmmmm => {
72             ("enum", vec!["id", "qualname", "scopeid", "value"], true, true)
73         }
74         Variantttttttttttttttttttttttt => {
75             ("variant", vec!["id", "name", "qualname", "type", "value", "scopeid"], true, true)
76         }
77     };
78
79     match x {
80         y => {
81             // Block with comment. Preserve me.
82         }
83         z => {
84             stmt();
85         }
86     }
87 }
88
89 fn matches() {
90     match 1 {
91         -1 => 10,
92         1 => 1, // foo
93         2 => 2,
94         // bar
95         3 => 3,
96         _ => 0, // baz
97     }
98 }
99
100 fn match_skip() {
101     let _ = match Some(1) {
102         #[rustfmt_skip]
103         Some( n ) => n,
104         None => 1,
105     };
106 }
107
108 fn issue339() {
109     match a {
110         b => {}
111         c => {}
112         d => {}
113         e => {}
114         // collapsing here is safe
115         ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff => {}
116         // collapsing here exceeds line length
117         ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffg => {
118         }
119         h => {
120             // comment above block
121         }
122         i => {} // comment below block
123         j => {
124             // comment inside block
125         }
126         j2 => {
127             // comments inside...
128         } // ... and after
129         // TODO uncomment when vertical whitespace is handled better
130         // k => {
131         //
132         //     // comment with WS above
133         // }
134         // l => {
135         //     // comment with ws below
136         //
137         // }
138         m => {}
139         n => {}
140         o => {}
141         p => {
142             // Dont collapse me
143         }
144         q => {}
145         r => {}
146         s => 0, // s comment
147         // t comment
148         t => 1,
149         u => 2,
150         // TODO uncomment when block-support exists
151         // v => {
152         // } /* funky block
153         //    * comment */
154         // final comment
155     }
156 }
157
158 fn issue355() {
159     match mac {
160         a => println!("a", b),
161         b => vec![1, 2],
162         c => vec!(3; 4),
163         d => println!("a", b),
164         e => vec![1, 2],
165         f => vec!(3; 4),
166         h => println!("a", b), // h comment
167         i => vec![1, 2], // i comment
168         j => vec!(3; 4), // j comment
169         // k comment
170         k => println!("a", b),
171         // l comment
172         l => vec![1, 2],
173         // m comment
174         m => vec!(3; 4),
175         // Rewrite splits macro
176         nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn => {
177             println!("a", b)
178         }
179         // Rewrite splits macro
180         oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo => {
181             vec![1, 2]
182         }
183         // Macro support fails to recognise this macro as splitable
184         // We push the whole expr to a new line, TODO split this macro as well
185         pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp => {
186             vec!(3; 4)
187         }
188         // q, r and s: Rewrite splits match arm
189         qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq => {
190             println!("a", b)
191         }
192         rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr => {
193             vec![1, 2]
194         }
195         ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss => {
196             vec!(3; 4)
197         }
198         // Funky bracketing styles
199         t => println!{"a", b},
200         u => vec![1, 2],
201         v => vec!{3; 4},
202         w => println!["a", b],
203         x => vec![1, 2],
204         y => vec![3; 4],
205         // Brackets with comments
206         tc => println!{"a", b}, // comment
207         uc => vec![1, 2], // comment
208         vc => vec!{3; 4}, // comment
209         wc => println!["a", b], // comment
210         xc => vec![1, 2], // comment
211         yc => vec![3; 4], // comment
212         yd => {
213             looooooooooooooooooooooooooooooooooooooooooooooooooooooooong_func(aaaaaaaaaa,
214                                                                               bbbbbbbbbb,
215                                                                               cccccccccc,
216                                                                               dddddddddd)
217         }
218     }
219 }
220
221 fn issue280() {
222     {
223         match x {
224             CompressionMode::DiscardNewline |
225             CompressionMode::CompressWhitespaceNewline => ch == '\n',
226             ast::ItemConst(ref typ, ref expr) => {
227                 self.process_static_or_const_item(item, &typ, &expr)
228             }
229         }
230     }
231 }
232
233 fn issue383() {
234     match resolution.last_private {
235         LastImport { .. } => false,
236         _ => true,
237     };
238 }
239
240 fn issue507() {
241     match 1 {
242         1 => unsafe { std::intrinsics::abort() },
243         _ => (),
244     }
245 }
246
247 fn issue508() {
248     match s.type_id() {
249         Some(NodeTypeId::Element(ElementTypeId::HTMLElement(
250                     HTMLElementTypeId::HTMLCanvasElement))) => true,
251         Some(NodeTypeId::Element(ElementTypeId::HTMLElement(
252                         HTMLElementTypeId::HTMLObjectElement))) => s.has_object_data(),
253         Some(NodeTypeId::Element(_)) => false,
254     }
255 }
256
257 fn issue496() {
258     {
259         {
260             {
261                 match def {
262                     def::DefConst(def_id) |
263                     def::DefAssociatedConst(def_id) => {
264                         match const_eval::lookup_const_by_id(cx.tcx, def_id, Some(self.pat.id)) {
265                             Some(const_expr) => x,
266                         }
267                     }
268                 }
269             }
270         }
271     }
272 }
273
274 fn issue494() {
275     {
276         match stmt.node {
277             hir::StmtExpr(ref expr, id) |
278             hir::StmtSemi(ref expr, id) => {
279                 result.push(StmtRef::Mirror(Box::new(Stmt {
280                     span: stmt.span,
281                     kind: StmtKind::Expr {
282                         scope: cx.tcx.region_maps.node_extent(id),
283                         expr: expr.to_ref(),
284                     },
285                 })))
286             }
287         }
288     }
289 }
290
291 fn issue386() {
292     match foo {
293         BiEq | BiLt | BiLe | BiNe | BiGt | BiGe => true,
294         BiAnd | BiOr | BiAdd | BiSub | BiMul | BiDiv | BiRem | BiBitXor | BiBitAnd | BiBitOr |
295         BiShl | BiShr => false,
296     }
297 }
298
299 fn guards() {
300     match foo {
301         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa if foooooooooooooo &&
302                                                                       barrrrrrrrrrrr => {}
303         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
304         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa if foooooooooooooo &&
305                                                                       barrrrrrrrrrrr => {}
306         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
307             if fooooooooooooooooooooo &&
308                (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ||
309                 cccccccccccccccccccccccccccccccccccccccc) => {}
310     }
311 }