mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: fix missing check for wrong assignment: non-option to option type (#17628)
This commit is contained in:
parent
d290f432d1
commit
39e80afab0
@ -208,6 +208,10 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
|
||||
} else {
|
||||
// Make sure the variable is mutable
|
||||
c.fail_if_immutable(left)
|
||||
|
||||
if !is_blank_ident && !left_type.has_flag(.option) && right_type.has_flag(.option) {
|
||||
c.error('cannot assign an Option value to a non-option variable', right.pos())
|
||||
}
|
||||
// left_type = c.expr(left)
|
||||
// if right is ast.None && !left_type.has_flag(.option) {
|
||||
// println(left_type)
|
||||
|
7
vlib/v/checker/tests/option_var_assign_err.out
Normal file
7
vlib/v/checker/tests/option_var_assign_err.out
Normal file
@ -0,0 +1,7 @@
|
||||
vlib/v/checker/tests/option_var_assign_err.vv:7:12: error: cannot assign an Option value to a non-option variable
|
||||
5 |
|
||||
6 | if value == 1 {
|
||||
7 | result = trying_to_change_non_opt_to_option(0)
|
||||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
8 | }
|
||||
9 | return result
|
14
vlib/v/checker/tests/option_var_assign_err.vv
Normal file
14
vlib/v/checker/tests/option_var_assign_err.vv
Normal file
@ -0,0 +1,14 @@
|
||||
module main
|
||||
|
||||
fn trying_to_change_non_opt_to_option(value int) ?int {
|
||||
mut result := 122
|
||||
|
||||
if value == 1 {
|
||||
result = trying_to_change_non_opt_to_option(0)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println(trying_to_change_non_opt_to_option(0))
|
||||
}
|
Loading…
Reference in New Issue
Block a user