From d257e439327223b00c137e73a76d9091f1f7b6ee Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 28 Nov 2022 16:29:02 +0800 Subject: [PATCH] checker: fix 'return none' in void optional function (#16545) --- vlib/v/checker/return.v | 4 ---- vlib/v/tests/option_void_2_test.v | 12 ++++++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 vlib/v/tests/option_void_2_test.v diff --git a/vlib/v/checker/return.v b/vlib/v/checker/return.v index 8324a9e0c2..3fa92bef58 100644 --- a/vlib/v/checker/return.v +++ b/vlib/v/checker/return.v @@ -93,10 +93,6 @@ fn (mut c Checker) return_stmt(mut node ast.Return) { if (exp_is_optional && got_types_0_idx in [ast.none_type_idx, ast.error_type_idx, option_type_idx]) || (exp_is_result && got_types_0_idx in [ast.error_type_idx, result_type_idx]) { - if got_types_0_idx == ast.none_type_idx && expected_type == ast.ovoid_type { - c.error('returning `none` in functions, that have a `?` result type is not allowed anymore, either `return error(message)` or just `return` instead', - node.pos) - } return } if expected_types.len > 0 && expected_types.len != got_types.len { diff --git a/vlib/v/tests/option_void_2_test.v b/vlib/v/tests/option_void_2_test.v new file mode 100644 index 0000000000..621d354081 --- /dev/null +++ b/vlib/v/tests/option_void_2_test.v @@ -0,0 +1,12 @@ +fn test_optional_void() { + foo(22)? + assert true +} + +fn foo(n int) ? { + if n > 0 { + println(n) + } else { + return none + } +}