mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: prevent string(byte), suggest byte.str() instead
This commit is contained in:
parent
839bd0e150
commit
37d739684c
@ -2126,6 +2126,9 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type {
|
||||
node.expr_type = c.expr(node.expr)
|
||||
from_type_sym := c.table.get_type_symbol(node.expr_type)
|
||||
to_type_sym := c.table.get_type_symbol(node.typ)
|
||||
if node.expr_type == table.byte_type && to_type_sym.kind == .string {
|
||||
c.error('can not cast type `byte` to string, use `${node.expr.str()}.str()` instead.', node.pos)
|
||||
}
|
||||
if to_type_sym.kind == .sum_type {
|
||||
if node.expr_type in [table.any_int_type, table.any_flt_type] {
|
||||
node.expr_type = c.promote_num(node.expr_type, if node.expr_type == table.any_int_type { table.int_type } else { table.f64_type })
|
||||
|
5
vlib/v/checker/tests/cast_string_with_byte_err.out
Normal file
5
vlib/v/checker/tests/cast_string_with_byte_err.out
Normal file
@ -0,0 +1,5 @@
|
||||
vlib/v/checker/tests/cast_string_with_byte_err.v:2:19: error: can not cast type `byte` to string, use `by.str()` instead.
|
||||
1 | for by in 'abc' {
|
||||
2 | println(string(by))
|
||||
| ~~
|
||||
3 | }
|
3
vlib/v/checker/tests/cast_string_with_byte_err.vv
Normal file
3
vlib/v/checker/tests/cast_string_with_byte_err.vv
Normal file
@ -0,0 +1,3 @@
|
||||
for by in 'abc' {
|
||||
println(string(by))
|
||||
}
|
Loading…
Reference in New Issue
Block a user