Skip to content

Commit be53b02

Browse files
authored
checker: require else branch in [flag] enum match (#19375)
1 parent 74d80a5 commit be53b02

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

vlib/v/checker/match.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,9 @@ fn (mut c Checker) match_exprs(mut node ast.MatchExpr, cond_type_sym ast.TypeSym
396396
unhandled << '`.${v}`'
397397
}
398398
}
399+
if cond_type_sym.info.is_flag {
400+
is_exhaustive = false
401+
}
399402
}
400403
else {
401404
is_exhaustive = false
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
vlib/v/checker/tests/flag_enum_exhaustive_else_branch.vv:11:1: error: match must be exhaustive (add `else {}` at the end)
2+
9 | dump(perm)
3+
10 |
4+
11 | match perm {
5+
| ~~~~~~~~~~~~
6+
12 | .read { println('r') }
7+
13 | .write { println('w') }
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[flag]
2+
enum Permission {
3+
read
4+
write
5+
execute
6+
}
7+
8+
perm := Permission.read | Permission.write | Permission.execute
9+
dump(perm)
10+
11+
match perm {
12+
.read { println('r') }
13+
.write { println('w') }
14+
.execute { println('x') }
15+
// must require an `else` here
16+
}

0 commit comments

Comments
 (0)