Skip to content

Commit

Permalink
checker: require else branch in [flag] enum match (#19375)
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed Sep 18, 2023
1 parent 74d80a5 commit be53b02
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions vlib/v/checker/match.v
Expand Up @@ -396,6 +396,9 @@ fn (mut c Checker) match_exprs(mut node ast.MatchExpr, cond_type_sym ast.TypeSym
unhandled << '`.${v}`'
}
}
if cond_type_sym.info.is_flag {
is_exhaustive = false
}
}
else {
is_exhaustive = false
Expand Down
7 changes: 7 additions & 0 deletions vlib/v/checker/tests/flag_enum_exhaustive_else_branch.out
@@ -0,0 +1,7 @@
vlib/v/checker/tests/flag_enum_exhaustive_else_branch.vv:11:1: error: match must be exhaustive (add `else {}` at the end)
9 | dump(perm)
10 |
11 | match perm {
| ~~~~~~~~~~~~
12 | .read { println('r') }
13 | .write { println('w') }
16 changes: 16 additions & 0 deletions vlib/v/checker/tests/flag_enum_exhaustive_else_branch.vv
@@ -0,0 +1,16 @@
[flag]
enum Permission {
read
write
execute
}

perm := Permission.read | Permission.write | Permission.execute
dump(perm)

match perm {
.read { println('r') }
.write { println('w') }
.execute { println('x') }
// must require an `else` here
}

0 comments on commit be53b02

Please sign in to comment.