diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index cfe00b30b7..e817eaa8b5 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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()) } } } diff --git a/vlib/v/checker/tests/if_expr_optional_err.out b/vlib/v/checker/tests/if_expr_optional_err.out new file mode 100644 index 0000000000..9adbc4a24d --- /dev/null +++ b/vlib/v/checker/tests/if_expr_optional_err.out @@ -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 | } diff --git a/vlib/v/checker/tests/if_expr_optional_err.vv b/vlib/v/checker/tests/if_expr_optional_err.vv new file mode 100644 index 0000000000..88d617d430 --- /dev/null +++ b/vlib/v/checker/tests/if_expr_optional_err.vv @@ -0,0 +1,10 @@ +fn get_bool() ?bool { + return true +} + +fn main() { + + if get_bool() { + println("Using plain lists") + } +}