diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 4ce47a6648..f9d643c7f2 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3446,6 +3446,11 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type { c.warn('casting `$ft` to `$tt` is only allowed in `unsafe` code', node.pos) } else if from_type_sym.kind == .array_fixed && !from_type.is_ptr() { c.warn('cannot cast a fixed array (use e.g. `&arr[0]` instead)', node.pos) + } else if from_type_sym_final.kind == .string && to_type_sym_final.is_number() + && to_type_sym_final.kind != .rune { + snexpr := node.expr.str() + c.error('cannot cast string to `$to_type_sym.name`, use `${snexpr}.${to_type_sym_final.name}()` instead.', + node.pos) } if to_type == ast.string_type { diff --git a/vlib/v/checker/tests/cast_string_to_int_err.out b/vlib/v/checker/tests/cast_string_to_int_err.out new file mode 100644 index 0000000000..7076b96f02 --- /dev/null +++ b/vlib/v/checker/tests/cast_string_to_int_err.out @@ -0,0 +1,61 @@ +vlib/v/checker/tests/cast_string_to_int_err.vv:2:7: error: cannot cast string to `int`, use `'456'.int()` instead. + 1 | fn main(){ + 2 | _ := int('456') + | ~~~~~~~~~~ + 3 | _ := i8('11') + 4 | _ := i16('111') +vlib/v/checker/tests/cast_string_to_int_err.vv:3:7: error: cannot cast string to `i8`, use `'11'.i8()` instead. + 1 | fn main(){ + 2 | _ := int('456') + 3 | _ := i8('11') + | ~~~~~~~~ + 4 | _ := i16('111') + 5 | _ := i64('234') +vlib/v/checker/tests/cast_string_to_int_err.vv:4:7: error: cannot cast string to `i16`, use `'111'.i16()` instead. + 2 | _ := int('456') + 3 | _ := i8('11') + 4 | _ := i16('111') + | ~~~~~~~~~~ + 5 | _ := i64('234') + 6 | _ := f32('2.22') +vlib/v/checker/tests/cast_string_to_int_err.vv:5:7: error: cannot cast string to `i64`, use `'234'.i64()` instead. + 3 | _ := i8('11') + 4 | _ := i16('111') + 5 | _ := i64('234') + | ~~~~~~~~~~ + 6 | _ := f32('2.22') + 7 | _ := f64('2.33333') +vlib/v/checker/tests/cast_string_to_int_err.vv:6:7: error: cannot cast string to `f32`, use `'2.22'.f32()` instead. + 4 | _ := i16('111') + 5 | _ := i64('234') + 6 | _ := f32('2.22') + | ~~~~~~~~~~~ + 7 | _ := f64('2.33333') + 8 | _ := u16('234') +vlib/v/checker/tests/cast_string_to_int_err.vv:7:7: error: cannot cast string to `f64`, use `'2.33333'.f64()` instead. + 5 | _ := i64('234') + 6 | _ := f32('2.22') + 7 | _ := f64('2.33333') + | ~~~~~~~~~~~~~~ + 8 | _ := u16('234') + 9 | _ := u32('2') +vlib/v/checker/tests/cast_string_to_int_err.vv:8:7: error: cannot cast string to `u16`, use `'234'.u16()` instead. + 6 | _ := f32('2.22') + 7 | _ := f64('2.33333') + 8 | _ := u16('234') + | ~~~~~~~~~~ + 9 | _ := u32('2') + 10 | _ := u64('22') +vlib/v/checker/tests/cast_string_to_int_err.vv:9:7: error: cannot cast string to `u32`, use `'2'.u32()` instead. + 7 | _ := f64('2.33333') + 8 | _ := u16('234') + 9 | _ := u32('2') + | ~~~~~~~~ + 10 | _ := u64('22') + 11 | } +vlib/v/checker/tests/cast_string_to_int_err.vv:10:7: error: cannot cast string to `u64`, use `'22'.u64()` instead. + 8 | _ := u16('234') + 9 | _ := u32('2') + 10 | _ := u64('22') + | ~~~~~~~~~ + 11 | } diff --git a/vlib/v/checker/tests/cast_string_to_int_err.vv b/vlib/v/checker/tests/cast_string_to_int_err.vv new file mode 100644 index 0000000000..bff8973a98 --- /dev/null +++ b/vlib/v/checker/tests/cast_string_to_int_err.vv @@ -0,0 +1,11 @@ +fn main(){ + _ := int('456') + _ := i8('11') + _ := i16('111') + _ := i64('234') + _ := f32('2.22') + _ := f64('2.33333') + _ := u16('234') + _ := u32('2') + _ := u64('22') +}