Skip to content

Commit

Permalink
Improve handling of commas after match arms.
Browse files Browse the repository at this point in the history
Fixes #507. Fixes #508.
  • Loading branch information
eefriedman committed Oct 21, 2015
1 parent ca50af7 commit e720218
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/expr.rs
Expand Up @@ -779,6 +779,7 @@ fn rewrite_match(context: &RewriteContext,
// We couldn't format the arm, just reproduce the source.
let snippet = context.snippet(mk_sp(arm_start_pos(arm), arm_end_pos(arm)));
result.push_str(&snippet);
result.push_str(arm_comma(&arm.body));
}
}
// BytePos(1) = closing match brace.
Expand Down Expand Up @@ -809,6 +810,18 @@ fn arm_end_pos(arm: &ast::Arm) -> BytePos {
arm.body.span.hi
}

fn arm_comma(body: &ast::Expr) -> &'static str {
if let ast::ExprBlock(ref block) = body.node {
if let ast::DefaultBlock = block.rules {
""
} else {
","
}
} else {
","
}
}

// Match arms.
impl Rewrite for ast::Arm {
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
Expand Down Expand Up @@ -881,11 +894,7 @@ impl Rewrite for ast::Arm {
line_start += offset.width();
}

let comma = if let ast::ExprBlock(_) = body.node {
""
} else {
","
};
let comma = arm_comma(body);

// Let's try and get the arm body on the same line as the condition.
// 4 = ` => `.len()
Expand Down
17 changes: 17 additions & 0 deletions tests/source/match.rs
Expand Up @@ -226,3 +226,20 @@ fn issue280() {
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,
}
}
17 changes: 17 additions & 0 deletions tests/target/match.rs
Expand Up @@ -230,3 +230,20 @@ fn issue383() {
_ => 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,
}
}

0 comments on commit e720218

Please sign in to comment.