checker: check error for infix compare optional (#13701)

pull/13705/head
yuyi 2022-03-10 02:20:21 +08:00 committed by GitHub
parent 54de04a916
commit 4c33003f86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 0 deletions

View File

@ -842,6 +842,9 @@ pub fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
}
}
}
} else if left_type.has_flag(.optional) && right_type.has_flag(.optional) {
c.error('unwrapped optional cannot be compared in an infix expression',
left_right_pos)
}
}
.left_shift {

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/infix_compare_optional_err.vv:6:5: error: unwrapped optional cannot be compared in an infix expression
4 |
5 | fn main(){
6 | if foo() > foo() {}
| ~~~~~~~~~~~~~
7 | }

View File

@ -0,0 +1,7 @@
fn foo() ?int{
return 0
}
fn main(){
if foo() > foo() {}
}