1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: check string cast to number (fix #12982) (#12983)

This commit is contained in:
yuyi 2021-12-28 13:55:39 +08:00 committed by GitHub
parent 6176ce9f03
commit c1711b8f05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 77 additions and 0 deletions

View File

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

View File

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

View File

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