checker: prohibit casting `void` (#10690)

pull/10698/head
shadowninja55 2021-07-07 14:59:58 -04:00 committed by GitHub
parent 6436d9a827
commit 806d6172cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 11 additions and 2 deletions

View File

@ -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)

View File

@ -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 ? {

View File

@ -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)',

View File

@ -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)

View File

@ -0,0 +1,2 @@
num := int(print(''))
println(num)