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