Skip to content

Commit

Permalink
checker: disallow none as match cond (#20688)
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed Jan 31, 2024
1 parent 1dc5d36 commit 74d4081
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions vlib/v/checker/match.v
Expand Up @@ -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?`',
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 branch.stmts.len > 0 {
mut stmt := branch.stmts.last()
Expand Down
5 changes: 5 additions & 0 deletions vlib/v/checker/tests/none_match_cond_err.out
@@ -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 {}
4 changes: 4 additions & 0 deletions vlib/v/checker/tests/none_match_cond_err.vv
@@ -0,0 +1,4 @@
match none {
none {}
else {}
}

0 comments on commit 74d4081

Please sign in to comment.