]> git.lizzy.rs Git - rust.git/blob - tests/target/match.rs
Merge pull request #681 from rust-lang-nursery/comment-style
[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 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         // TODO uncomment when block-support exists
161         // v => {
162         // } /* funky block
163         //    * comment */
164         // final comment
165     }
166 }
167
168 fn issue355() {
169     match mac {
170         a => println!("a", b),
171         b => vec![1, 2],
172         c => vec!(3; 4),
173         d => println!("a", b),
174         e => vec![1, 2],
175         f => vec!(3; 4),
176         h => println!("a", b), // h comment
177         i => vec![1, 2], // i comment
178         j => vec!(3; 4), // j comment
179         // k comment
180         k => println!("a", b),
181         // l comment
182         l => vec![1, 2],
183         // m comment
184         m => vec!(3; 4),
185         // Rewrite splits macro
186         nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn => {
187             println!("a", b)
188         }
189         // Rewrite splits macro
190         oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo => {
191             vec![1, 2]
192         }
193         // Macro support fails to recognise this macro as splitable
194         // We push the whole expr to a new line, TODO split this macro as well
195         pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp => {
196             vec!(3; 4)
197         }
198         // q, r and s: Rewrite splits match arm
199         qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq => {
200             println!("a", b)
201         }
202         rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr => {
203             vec![1, 2]
204         }
205         ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss => {
206             vec!(3; 4)
207         }
208         // Funky bracketing styles
209         t => println!{"a", b},
210         u => vec![1, 2],
211         v => vec!{3; 4},
212         w => println!["a", b],
213         x => vec![1, 2],
214         y => vec![3; 4],
215         // Brackets with comments
216         tc => println!{"a", b}, // comment
217         uc => vec![1, 2], // comment
218         vc => vec!{3; 4}, // comment
219         wc => println!["a", b], // comment
220         xc => vec![1, 2], // comment
221         yc => vec![3; 4], // comment
222         yd => {
223             looooooooooooooooooooooooooooooooooooooooooooooooooooooooong_func(aaaaaaaaaa,
224                                                                               bbbbbbbbbb,
225                                                                               cccccccccc,
226                                                                               dddddddddd)
227         }
228     }
229 }
230
231 fn issue280() {
232     {
233         match x {
234             CompressionMode::DiscardNewline | CompressionMode::CompressWhitespaceNewline => {
235                 ch == '\n'
236             }
237             ast::ItemConst(ref typ, ref expr) => {
238                 self.process_static_or_const_item(item, &typ, &expr)
239             }
240         }
241     }
242 }
243
244 fn issue383() {
245     match resolution.last_private {
246         LastImport{..} => false,
247         _ => true,
248     };
249 }
250
251 fn issue507() {
252     match 1 {
253         1 => unsafe { std::intrinsics::abort() },
254         _ => (),
255     }
256 }
257
258 fn issue508() {
259     match s.type_id() {
260         Some(NodeTypeId::Element(ElementTypeId::HTMLElement(
261                     HTMLElementTypeId::HTMLCanvasElement))) => true,
262         Some(NodeTypeId::Element(ElementTypeId::HTMLElement(
263                         HTMLElementTypeId::HTMLObjectElement))) => 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 }