From 806d6172cb61f9a413f05434c34bbcb213774704 Mon Sep 17 00:00:00 2001 From: shadowninja55 <49539636+shadowninja55@users.noreply.github.com> Date: Wed, 7 Jul 2021 14:59:58 -0400 Subject: [PATCH] checker: prohibit casting `void` (#10690) --- vlib/builtin/cfns.c.v | 2 +- vlib/net/http/backend_windows.c.v | 2 +- vlib/v/checker/checker.v | 3 +++ vlib/v/checker/tests/cast_void.out | 4 ++++ vlib/v/checker/tests/cast_void.vv | 2 ++ 5 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 vlib/v/checker/tests/cast_void.out create mode 100644 vlib/v/checker/tests/cast_void.vv diff --git a/vlib/builtin/cfns.c.v b/vlib/builtin/cfns.c.v index ebe7deaf71..b67da34422 100644 --- a/vlib/builtin/cfns.c.v +++ b/vlib/builtin/cfns.c.v @@ -343,7 +343,7 @@ fn C.closesocket(int) int fn C.vschannel_init(&C.TlsContext) -fn C.request(&C.TlsContext, int, &u16, &byte, &&byte) +fn C.request(&C.TlsContext, int, &u16, &byte, &&byte) int fn C.vschannel_cleanup(&C.TlsContext) diff --git a/vlib/net/http/backend_windows.c.v b/vlib/net/http/backend_windows.c.v index ffa8ce1dfa..9181166098 100644 --- a/vlib/net/http/backend_windows.c.v +++ b/vlib/net/http/backend_windows.c.v @@ -18,7 +18,7 @@ fn (req &Request) ssl_do(port int, method Method, host_name string, path string) $if trace_http_request ? { eprintln('> $sdata') } - length := int(C.request(&ctx, port, addr.to_wide(), sdata.str, &buff)) + length := C.request(&ctx, port, addr.to_wide(), sdata.str, &buff) C.vschannel_cleanup(&ctx) response_text := unsafe { buff.vstring_with_len(length) } $if trace_http_response ? { diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 143a4ce9e7..94d3e05924 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -5114,6 +5114,9 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type { } n_e_t_idx := node.expr_type.idx() expr_is_ptr := node.expr_type.is_ptr() || n_e_t_idx in ast.pointer_type_idxs + if node.expr_type == ast.void_type { + c.error('expression does not return a value so it cannot be casted', node.expr.position()) + } if expr_is_ptr && to_type_sym.kind == .string && !node.in_prexpr { if node.has_arg { c.warn('to convert a C string buffer pointer to a V string, use x.vstring_with_len(len) instead of string(x,len)', diff --git a/vlib/v/checker/tests/cast_void.out b/vlib/v/checker/tests/cast_void.out new file mode 100644 index 0000000000..5ca6a9beca --- /dev/null +++ b/vlib/v/checker/tests/cast_void.out @@ -0,0 +1,4 @@ +vlib/v/checker/tests/cast_void.vv:1:12: error: expression does not return a value so it cannot be casted + 1 | num := int(print('')) + | ~~~~~~~~~ + 2 | println(num) diff --git a/vlib/v/checker/tests/cast_void.vv b/vlib/v/checker/tests/cast_void.vv new file mode 100644 index 0000000000..1b61f14223 --- /dev/null +++ b/vlib/v/checker/tests/cast_void.vv @@ -0,0 +1,2 @@ +num := int(print('')) +println(num)