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