zig fmt: switch with empty body

master
Andrew Kelley 2018-05-04 16:49:51 -04:00
parent eef21df94f
commit 0fc8885a8d
2 changed files with 16 additions and 4 deletions

View File

@ -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;

View File

@ -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(