checker: check if condition optional (#10921)
parent
fe5e3c452f
commit
a2de3ffcdb
|
@ -6498,9 +6498,10 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
|
|||
// check condition type is boolean
|
||||
c.expected_type = ast.bool_type
|
||||
cond_typ := c.expr(branch.cond)
|
||||
if cond_typ.idx() != ast.bool_type_idx && !c.pref.translated {
|
||||
typ_sym := c.table.get_type_symbol(cond_typ)
|
||||
c.error('non-bool type `$typ_sym.name` used as if condition', branch.cond.position())
|
||||
if (cond_typ.idx() != ast.bool_type_idx || cond_typ.has_flag(.optional))
|
||||
&& !c.pref.translated {
|
||||
c.error('non-bool type `${c.table.type_to_str(cond_typ)}` used as if condition',
|
||||
branch.cond.position())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
vlib/v/checker/tests/if_expr_optional_err.vv:7:5: error: non-bool type `?bool` used as if condition
|
||||
5 | fn main() {
|
||||
6 |
|
||||
7 | if get_bool() {
|
||||
| ~~~~~~~~~~
|
||||
8 | println("Using plain lists")
|
||||
9 | }
|
|
@ -0,0 +1,10 @@
|
|||
fn get_bool() ?bool {
|
||||
return true
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
if get_bool() {
|
||||
println("Using plain lists")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue