1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: fix non-bool check on use of result bool (fix #15539) (#15540)

This commit is contained in:
ChAoS_UnItY 2022-08-26 12:08:57 +08:00 committed by GitHub
parent b83dd86d65
commit b0e7ddfd97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 8 deletions

View File

@ -47,8 +47,8 @@ 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.unwrap_generic(c.expr(branch.cond))
if (cond_typ.idx() != ast.bool_type_idx || cond_typ.has_flag(.optional))
&& !c.pref.translated && !c.file.is_translated {
if (cond_typ.idx() != ast.bool_type_idx || cond_typ.has_flag(.optional)
|| cond_typ.has_flag(.result)) && !c.pref.translated && !c.file.is_translated {
c.error('non-bool type `${c.table.type_to_str(cond_typ)}` used as if condition',
branch.cond.pos())
}

View File

@ -1,7 +1,7 @@
vlib/v/checker/tests/if_expr_optional_err.vv:7:5: error: non-bool type `?bool` used as if condition
vlib/v/checker/tests/if_expr_optional_err.vv:6:5: error: non-bool type `?bool` used as if condition
4 |
5 | fn main() {
6 |
7 | if get_bool() {
6 | if get_bool() {
| ~~~~~~~~~~
8 | println("Using plain lists")
9 | }
7 | println("Using plain lists")
8 | }

View File

@ -3,7 +3,6 @@ fn get_bool() ?bool {
}
fn main() {
if get_bool() {
println("Using plain lists")
}

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/if_expr_result_err.vv:6:5: error: non-bool type `!bool` used as if condition
4 |
5 | fn main() {
6 | if get_bool() {
| ~~~~~~~~~~
7 | println("Using plain lists")
8 | }

View File

@ -0,0 +1,9 @@
fn get_bool() !bool {
return true
}
fn main() {
if get_bool() {
println("Using plain lists")
}
}