From 65abfa8219315c76a3a53b6ba2be7ad39036b8e3 Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Fri, 7 Apr 2023 10:10:11 +0530 Subject: [PATCH] checker: disallow printing void (#17901) --- vlib/v/checker/fn.v | 1 + vlib/v/checker/tests/array_delete_print_err.out | 13 +++++++++++++ vlib/v/checker/tests/array_delete_print_err.vv | 5 +++++ 3 files changed, 19 insertions(+) create mode 100644 vlib/v/checker/tests/array_delete_print_err.out create mode 100644 vlib/v/checker/tests/array_delete_print_err.vv diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index e4145f3614..90f49b71d7 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -2436,6 +2436,7 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as if method := c.table.find_method(unwrapped_left_sym, method_name) { node.receiver_type = method.receiver_type } + node.return_type = ast.void_type } return node.return_type } diff --git a/vlib/v/checker/tests/array_delete_print_err.out b/vlib/v/checker/tests/array_delete_print_err.out new file mode 100644 index 0000000000..94b39565f4 --- /dev/null +++ b/vlib/v/checker/tests/array_delete_print_err.out @@ -0,0 +1,13 @@ +vlib/v/checker/tests/array_delete_print_err.vv:3:2: error: `println` can not print void expressions + 1 | fn main() { + 2 | a := []string{} + 3 | println(a.delete(0)) + | ~~~~~~~~~~~~~~~~~~~~ + 4 | println('${a.delete(0)}') + 5 | } +vlib/v/checker/tests/array_delete_print_err.vv:4:15: error: expression does not return a value + 2 | a := []string{} + 3 | println(a.delete(0)) + 4 | println('${a.delete(0)}') + | ~~~~~~~~~ + 5 | } diff --git a/vlib/v/checker/tests/array_delete_print_err.vv b/vlib/v/checker/tests/array_delete_print_err.vv new file mode 100644 index 0000000000..79268cdeb2 --- /dev/null +++ b/vlib/v/checker/tests/array_delete_print_err.vv @@ -0,0 +1,5 @@ +fn main() { + a := []string{} + println(a.delete(0)) + println('${a.delete(0)}') +}