checker: require else branch in [flag] enum match (#19375)

This commit is contained in:
Swastik Baranwal 2023-09-18 18:37:30 +05:30 committed by GitHub
parent 74d80a5120
commit be53b02511
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 0 deletions

View File

@ -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

View File

@ -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') }

View File

@ -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
}