Skip to content

Commit 0fc8885

Browse files
committedMay 4, 2018
zig fmt: switch with empty body
·
0.15.20.3.0
1 parent eef21df commit 0fc8885

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed
 

‎std/zig/parser.zig‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4298,14 +4298,21 @@ pub const Parser = struct {
42984298
ast.Node.Id.DocComment => unreachable, // doc comments are attached to nodes
42994299
ast.Node.Id.Switch => {
43004300
const switch_node = @fieldParentPtr(ast.Node.Switch, "base", base);
4301+
const cases = switch_node.cases.toSliceConst();
4302+
43014303
try stream.print("{} (", self.tokenizer.getTokenSlice(switch_node.switch_token));
43024304

4305+
if (cases.len == 0) {
4306+
try stack.append(RenderState { .Text = ") {}"});
4307+
try stack.append(RenderState { .Expression = switch_node.expr });
4308+
continue;
4309+
}
4310+
43034311
try stack.append(RenderState { .Text = "}"});
43044312
try stack.append(RenderState.PrintIndent);
43054313
try stack.append(RenderState { .Indent = indent });
43064314
try stack.append(RenderState { .Text = "\n"});
43074315

4308-
const cases = switch_node.cases.toSliceConst();
43094316
var i = cases.len;
43104317
while (i != 0) {
43114318
i -= 1;

‎std/zig/parser_test.zig‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
//if (sr > n_uword_bits - 1) // d > r
33
// return 0;
44

5-
// TODO switch with no body
6-
// format(&size, error{}, countSize, fmt, args) catch |err| switch (err) {};
7-
5+
test "zig fmt: switch with empty body" {
6+
try testCanonical(
7+
\\test "" {
8+
\\ foo() catch |err| switch (err) {};
9+
\\}
10+
\\
11+
);
12+
}
813

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

0 commit comments

Comments
 (0)
Please sign in to comment.