diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 2405698217..eee52c6a32 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2492,6 +2492,10 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type { tt := c.table.type_to_str(to_type) c.error('cannot cast string to `$tt`, use `${snexpr}.${final_to_sym.name}()` instead.', node.pos) + } else if final_from_sym.kind == .string && to_type.is_ptr() && to_sym.kind != .string { + snexpr := node.expr.str() + tt := c.table.type_to_str(to_type) + c.error('cannot cast string to `$tt`, use `${snexpr}.str` instead.', node.pos) } if to_sym.kind == .rune && from_sym.is_string() { diff --git a/vlib/v/checker/tests/cast_string_to_ptr_err.out b/vlib/v/checker/tests/cast_string_to_ptr_err.out new file mode 100644 index 0000000000..5b966ea872 --- /dev/null +++ b/vlib/v/checker/tests/cast_string_to_ptr_err.out @@ -0,0 +1,5 @@ +vlib/v/checker/tests/cast_string_to_ptr_err.vv:2:7: error: cannot cast string to `&char`, use `'foo'.str` instead. + 1 | fn main() { + 2 | _ := &char('foo') + | ~~~~~~~~~~~~ + 3 | } diff --git a/vlib/v/checker/tests/cast_string_to_ptr_err.vv b/vlib/v/checker/tests/cast_string_to_ptr_err.vv new file mode 100644 index 0000000000..a43a136ade --- /dev/null +++ b/vlib/v/checker/tests/cast_string_to_ptr_err.vv @@ -0,0 +1,3 @@ +fn main() { + _ := &char('foo') +}