Skip to content

Commit

Permalink
AstGen: error on unused switch label
Browse files Browse the repository at this point in the history
  • Loading branch information
mlugg committed Aug 30, 2024
1 parent 6ab4c41 commit 9db70ba
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/std/zig/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7999,6 +7999,11 @@ fn switchExpr(
appendBodyWithFixupsArrayList(astgen, payloads, case_slice);
}
}

if (switch_full.label_token) |label_token| if (!block_scope.label.?.used) {
try astgen.appendErrorTok(label_token, "unused switch label", .{});
};

// Now that the item expressions are generated we can add this.
try parent_gz.instructions.append(gpa, switch_block);

Expand Down
6 changes: 6 additions & 0 deletions test/cases/compile_errors/duplicate-unused_labels.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ comptime {
comptime {
blk: for (@as([0]void, undefined)) |_| {}
}
comptime {
blk: switch (true) {
else => {},
}
}

// error
// target=native
Expand All @@ -35,3 +40,4 @@ comptime {
// :17:5: error: unused block label
// :20:5: error: unused while loop label
// :23:5: error: unused for loop label
// :26:5: error: unused switch label

0 comments on commit 9db70ba

Please sign in to comment.