Skip to content

Commit be0dc0e

Browse files
authored
checker: fix check omission in cast string to ptr. (fix #14921) (#15721)
1 parent 3e599a1 commit be0dc0e

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

vlib/v/checker/checker.v

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2492,6 +2492,10 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
24922492
tt := c.table.type_to_str(to_type)
24932493
c.error('cannot cast string to `$tt`, use `${snexpr}.${final_to_sym.name}()` instead.',
24942494
node.pos)
2495+
} else if final_from_sym.kind == .string && to_type.is_ptr() && to_sym.kind != .string {
2496+
snexpr := node.expr.str()
2497+
tt := c.table.type_to_str(to_type)
2498+
c.error('cannot cast string to `$tt`, use `${snexpr}.str` instead.', node.pos)
24952499
}
24962500

24972501
if to_sym.kind == .rune && from_sym.is_string() {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
vlib/v/checker/tests/cast_string_to_ptr_err.vv:2:7: error: cannot cast string to `&char`, use `'foo'.str` instead.
2+
1 | fn main() {
3+
2 | _ := &char('foo')
4+
| ~~~~~~~~~~~~
5+
3 | }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
_ := &char('foo')
3+
}

0 commit comments

Comments
 (0)