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