Skip to content

Commit

Permalink
zig fmt: switch with empty body
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrk committed May 4, 2018
1 parent eef21df commit 0fc8885
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
9 changes: 8 additions & 1 deletion std/zig/parser.zig
Expand Up @@ -4298,14 +4298,21 @@ pub const Parser = struct {
ast.Node.Id.DocComment => unreachable, // doc comments are attached to nodes
ast.Node.Id.Switch => {
const switch_node = @fieldParentPtr(ast.Node.Switch, "base", base);
const cases = switch_node.cases.toSliceConst();

try stream.print("{} (", self.tokenizer.getTokenSlice(switch_node.switch_token));

if (cases.len == 0) {
try stack.append(RenderState { .Text = ") {}"});
try stack.append(RenderState { .Expression = switch_node.expr });
continue;
}

try stack.append(RenderState { .Text = "}"});
try stack.append(RenderState.PrintIndent);
try stack.append(RenderState { .Indent = indent });
try stack.append(RenderState { .Text = "\n"});

const cases = switch_node.cases.toSliceConst();
var i = cases.len;
while (i != 0) {
i -= 1;
Expand Down
11 changes: 8 additions & 3 deletions std/zig/parser_test.zig
Expand Up @@ -2,9 +2,14 @@
//if (sr > n_uword_bits - 1) // d > r
// return 0;

// TODO switch with no body
// format(&size, error{}, countSize, fmt, args) catch |err| switch (err) {};

test "zig fmt: switch with empty body" {
try testCanonical(
\\test "" {
\\ foo() catch |err| switch (err) {};
\\}
\\
);
}

test "zig fmt: same-line comment on comptime expression" {
try testCanonical(
Expand Down

0 comments on commit 0fc8885

Please sign in to comment.