checker: check error for infix compare optional

pull/13701/head
yuyi98 2022-03-09 19:46:15 +08:00
parent 6c7243f2f7
commit 75c8768fe2
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() {}
}