checker: disallow none as match cond (#20688)

This commit is contained in:
Swastik Baranwal 2024-01-31 10:15:00 +05:30 committed by GitHub
parent 1dc5d36724
commit 74d4081f1b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 0 deletions

View File

@ -59,6 +59,9 @@ fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type {
c.error('`match` expression with Option type only checks against `none`, to match its value you must unwrap it first `var?`', c.error('`match` expression with Option type only checks against `none`, to match its value you must unwrap it first `var?`',
branch.pos) branch.pos)
} }
if cond_type_sym.kind == .none_ {
c.error('`none` cannot be a match condition', node.pos)
}
// If the last statement is an expression, return its type // If the last statement is an expression, return its type
if branch.stmts.len > 0 { if branch.stmts.len > 0 {
mut stmt := branch.stmts.last() mut stmt := branch.stmts.last()

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/none_match_cond_err.vv:1:1: error: `none` cannot be a match condition
1 | match none {
| ~~~~~~~~~~~~
2 | none {}
3 | else {}

View File

@ -0,0 +1,4 @@
match none {
none {}
else {}
}