Skip to content

Commit 9db70ba

Browse files
committed
AstGen: error on unused switch label
1 parent 6ab4c41 commit 9db70ba

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

lib/std/zig/AstGen.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7999,6 +7999,11 @@ fn switchExpr(
79997999
appendBodyWithFixupsArrayList(astgen, payloads, case_slice);
80008000
}
80018001
}
8002+
8003+
if (switch_full.label_token) |label_token| if (!block_scope.label.?.used) {
8004+
try astgen.appendErrorTok(label_token, "unused switch label", .{});
8005+
};
8006+
80028007
// Now that the item expressions are generated we can add this.
80038008
try parent_gz.instructions.append(gpa, switch_block);
80048009

test/cases/compile_errors/duplicate-unused_labels.zig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ comptime {
2222
comptime {
2323
blk: for (@as([0]void, undefined)) |_| {}
2424
}
25+
comptime {
26+
blk: switch (true) {
27+
else => {},
28+
}
29+
}
2530

2631
// error
2732
// target=native
@@ -35,3 +40,4 @@ comptime {
3540
// :17:5: error: unused block label
3641
// :20:5: error: unused while loop label
3742
// :23:5: error: unused for loop label
43+
// :26:5: error: unused switch label

0 commit comments

Comments
 (0)