diff --git a/std/zig/parser.zig b/std/zig/parser.zig index 31c49b27f..f303bccd8 100644 --- a/std/zig/parser.zig +++ b/std/zig/parser.zig @@ -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; diff --git a/std/zig/parser_test.zig b/std/zig/parser_test.zig index af32f2315..3971f45af 100644 --- a/std/zig/parser_test.zig +++ b/std/zig/parser_test.zig @@ -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(