checker: fix unchecked optional in match condition (#10041)

pull/10038/head^2
El Koulali András 2021-05-08 12:33:13 +02:00 committed by GitHub
parent 8a380f4699
commit 262ef7598d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 2 deletions

View File

@ -5206,6 +5206,7 @@ pub fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type {
// since it is used in c.match_exprs() it saves checking twice
node.cond_type = c.table.mktyp(cond_type)
c.ensure_type_exists(node.cond_type, node.pos) or { return ast.void_type }
c.check_expr_opt_call(node.cond, cond_type)
cond_type_sym := c.table.get_type_symbol(cond_type)
if cond_type_sym.kind !in [.interface_, .sum_type] {
node.is_sum_type = false

View File

@ -151,10 +151,18 @@ vlib/v/checker/tests/optional_fn_err.vv:69:18: error: bar() returns an option, s
69 | println(arr.any(bar(true)))
| ~~~~~~~~~
70 | println(arr.all(bar(true)))
71 | }
71 |
vlib/v/checker/tests/optional_fn_err.vv:70:18: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end
68 | println(arr.filter(bar(true)))
69 | println(arr.any(bar(true)))
70 | println(arr.all(bar(true)))
| ~~~~~~~~~
71 | }
71 |
72 | match bar(0) {
vlib/v/checker/tests/optional_fn_err.vv:72:8: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end
70 | println(arr.all(bar(true)))
71 |
72 | match bar(0) {
| ~~~~~~
73 | 0 { }
74 | else { }

View File

@ -68,4 +68,9 @@ fn main() {
println(arr.filter(bar(true)))
println(arr.any(bar(true)))
println(arr.all(bar(true)))
match bar(0) {
0 { }
else { }
}
}