rust/tests/target/match.rs

301 lines
8.3 KiB
Rust
Raw Normal View History

2015-08-14 10:00:22 +02:00
// Match expressions.
fn foo() {
// A match expression.
match x {
// Some comment.
a => foo(),
b if 0 < 42 => foo(),
2015-10-19 21:41:47 +02:00
c => {
// Another comment.
2015-08-14 10:00:22 +02:00
// Comment.
an_expression;
foo()
}
// Perhaps this should introduce braces?
2015-11-20 21:05:10 +01:00
Foo(ref bar) => {
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
}
2015-08-14 10:00:22 +02:00
Pattern1 | Pattern2 | Pattern3 => false,
Paternnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn |
Paternnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn => {
blah
}
Patternnnnnnnnnnnnnnnnnnn |
Patternnnnnnnnnnnnnnnnnnn |
Patternnnnnnnnnnnnnnnnnnn |
Patternnnnnnnnnnnnnnnnnnn => meh,
Patternnnnnnnnnnnnnnnnnnn |
Patternnnnnnnnnnnnnnnnnnn if looooooooooooooooooong_guard => meh,
Patternnnnnnnnnnnnnnnnnnnnnnnnn |
2015-11-20 21:05:10 +01:00
Patternnnnnnnnnnnnnnnnnnnnnnnnn if looooooooooooooooooooooooooooooooooooooooong_guard => {
meh
}
2015-08-16 23:41:45 +02:00
// Test that earlier patterns can take the guard space
2015-10-17 15:56:53 +02:00
(aaaa,
bbbbb,
ccccccc,
aaaaa,
bbbbbbbb,
cccccc,
aaaa,
bbbbbbbb,
cccccc,
dddddd) |
2015-08-16 23:41:45 +02:00
Patternnnnnnnnnnnnnnnnnnnnnnnnn if loooooooooooooooooooooooooooooooooooooooooong_guard => {}
2015-08-14 10:00:22 +02:00
_ => {}
ast::PathParameters::AngleBracketedParameters(ref data) if data.lifetimes.len() > 0 ||
data.types.len() > 0 ||
2015-09-24 02:22:06 +02:00
data.bindings.len() > 0 => {}
2015-08-14 10:00:22 +02:00
}
let whatever = match something {
/// DOC COMMENT!
Some(_) => 42,
// Comment on an attribute.
2015-08-14 10:00:22 +02:00
#[an_attribute]
2015-08-16 23:41:45 +02:00
// Comment after an attribute.
2015-08-14 10:00:22 +02:00
None => 0,
2015-08-16 23:41:45 +02:00
#[rustfmt_skip]
Blurb => { }
2015-08-14 10:00:22 +02:00
};
}
// Test that a match on an overflow line is laid out properly.
fn main() {
let sub_span =
2015-09-09 23:15:37 +02:00
match xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {
Some(sub_span) => Some(sub_span),
None => sub_span,
};
}
// Test that one-line bodies align.
fn main() {
match r {
2015-11-20 21:05:10 +01:00
Variableeeeeeeeeeeeeeeeee => {
("variable",
vec!["id", "name", "qualname", "value", "type", "scopeid"],
true,
true)
}
Enummmmmmmmmmmmmmmmmmmmm => {
("enum",
vec!["id", "qualname", "scopeid", "value"],
true,
true)
}
Variantttttttttttttttttttttttt => {
2015-09-26 23:16:11 +02:00
("variant",
2015-10-16 22:54:32 +02:00
vec!["id", "name", "qualname", "type", "value", "scopeid"],
2015-09-26 23:16:11 +02:00
true,
2015-11-20 21:05:10 +01:00
true)
}
}
}
fn matches() {
match 1 {
2015-10-17 15:56:53 +02:00
-1 => 10,
1 => 1, // foo
2 => 2,
// bar
3 => 3,
_ => 0, // baz
}
}
fn issue339() {
match a {
b => {}
c => {}
d => {}
e => {}
// collapsing here is safe
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff => {}
// collapsing here exceeds line length
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffg => {
}
2015-10-19 21:41:47 +02:00
h => {
// comment above block
}
i => {} // comment below block
j => {
// comment inside block
}
j2 => {
// comments inside...
} // ... and after
// TODO uncomment when vertical whitespace is handled better
// k => {
//
// // comment with WS above
// }
// l => {
// // comment with ws below
//
// }
m => {}
n => {}
o => {}
2015-10-19 21:41:47 +02:00
p => {
// Dont collapse me
}
q => {}
r => {}
s => 0, // s comment
// t comment
t => 1,
u => 2,
// TODO uncomment when block-support exists
// v => {
// } /* funky block
// * comment */
// final comment
}
}
fn issue355() {
match mac {
a => println!("a", b),
2015-10-16 22:54:32 +02:00
b => vec![1, 2],
c => vec!(3; 4),
d => {
println!("a", b)
}
e => {
2015-10-16 22:54:32 +02:00
vec![1, 2]
}
f => {
vec!(3; 4)
}
h => println!("a", b), // h comment
2015-10-16 22:54:32 +02:00
i => vec![1, 2], // i comment
j => vec!(3; 4), // j comment
// k comment
k => println!("a", b),
// l comment
2015-10-16 22:54:32 +02:00
l => vec![1, 2],
// m comment
m => vec!(3; 4),
// Rewrite splits macro
2015-11-20 21:05:10 +01:00
nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn => {
println!("a", b)
}
// Rewrite splits macro
2015-11-20 21:05:10 +01:00
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo => {
vec![1, 2]
}
// Macro support fails to recognise this macro as splitable
// We push the whole expr to a new line, TODO split this macro as well
2015-11-20 21:05:10 +01:00
pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp => {
vec!(3; 4)
}
// q, r and s: Rewrite splits match arm
2015-11-20 21:05:10 +01:00
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq => {
println!("a", b)
}
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr => {
vec![1, 2]
}
ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss => {
vec!(3; 4)
}
// Funky bracketing styles
t => println!{"a", b},
2015-10-16 22:54:32 +02:00
u => vec![1, 2],
v => vec!{3; 4},
w => println!["a", b],
x => vec![1, 2],
y => vec![3; 4],
// Brackets with comments
tc => println!{"a", b}, // comment
2015-10-16 22:54:32 +02:00
uc => vec![1, 2], // comment
vc => vec!{3; 4}, // comment
wc => println!["a", b], // comment
xc => vec![1, 2], // comment
yc => vec![3; 4], // comment
2015-11-20 21:05:10 +01:00
yd => {
looooooooooooooooooooooooooooooooooooooooooooooooooooooooong_func(aaaaaaaaaa,
bbbbbbbbbb,
cccccccccc,
dddddddddd)
}
}
}
fn issue280() {
{
match x {
2015-11-20 21:05:10 +01:00
CompressionMode::DiscardNewline | CompressionMode::CompressWhitespaceNewline => {
ch == '\n'
}
ast::ItemConst(ref typ, ref expr) => {
self.process_static_or_const_item(item, &typ, &expr)
}
}
}
}
fn issue383() {
match resolution.last_private {
LastImport{..} => false,
_ => true,
};
}
fn issue507() {
match 1 {
1 => unsafe { std::intrinsics::abort() },
_ => (),
}
}
fn issue508() {
match s.type_id() {
Some(NodeTypeId::Element(ElementTypeId::HTMLElement(
HTMLElementTypeId::HTMLCanvasElement))) => true,
Some(NodeTypeId::Element(ElementTypeId::HTMLElement(
HTMLElementTypeId::HTMLObjectElement))) => s.has_object_data(),
Some(NodeTypeId::Element(_)) => false,
}
}
2015-10-23 21:20:43 +02:00
fn issue496() {
{
{
{
match def {
2015-11-20 21:05:10 +01:00
def::DefConst(def_id) | def::DefAssociatedConst(def_id) => {
2015-10-23 21:20:43 +02:00
match const_eval::lookup_const_by_id(cx.tcx, def_id, Some(self.pat.id)) {
Some(const_expr) => {
x
}
2015-11-20 21:05:10 +01:00
}
}
2015-10-23 21:20:43 +02:00
}
}
}
}
}
fn issue494() {
{
match stmt.node {
2015-11-20 21:05:10 +01:00
hir::StmtExpr(ref expr, id) | hir::StmtSemi(ref expr, id) => {
2015-10-23 21:20:43 +02:00
result.push(StmtRef::Mirror(Box::new(Stmt {
span: stmt.span,
kind: StmtKind::Expr {
scope: cx.tcx.region_maps.node_extent(id),
expr: expr.to_ref(),
},
2015-11-20 21:05:10 +01:00
})))
}
2015-10-23 21:20:43 +02:00
}
}
}