mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: suggest using :=
when assigning to undefined variable (#5911)
This commit is contained in:
parent
8b930b4888
commit
f4251dded0
@ -2524,7 +2524,12 @@ pub fn (mut c Checker) ident(mut ident ast.Ident) table.Type {
|
||||
}
|
||||
ident.mod = saved_mod
|
||||
}
|
||||
c.error('undefined ident: `$ident.name`', ident.pos)
|
||||
if ident.tok_kind == .assign {
|
||||
c.error('undefined ident: `$ident.name` (use `:=` to assign a variable)',
|
||||
ident.pos)
|
||||
} else {
|
||||
c.error('undefined ident: `$ident.name`', ident.pos)
|
||||
}
|
||||
}
|
||||
if c.table.known_type(ident.name) {
|
||||
// e.g. `User` in `json.decode(User, '...')`
|
||||
|
5
vlib/v/checker/tests/unknown_var_assign.out
Normal file
5
vlib/v/checker/tests/unknown_var_assign.out
Normal file
@ -0,0 +1,5 @@
|
||||
vlib/v/checker/tests/unknown_var_assign.v:2:5: error: undefined ident: `x` (use `:=` to assign a variable)
|
||||
1 | fn main() {
|
||||
2 | x = 'hello v'
|
||||
| ^
|
||||
3 | }
|
3
vlib/v/checker/tests/unknown_var_assign.vv
Normal file
3
vlib/v/checker/tests/unknown_var_assign.vv
Normal file
@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
x = 'hello v'
|
||||
}
|
@ -850,6 +850,7 @@ pub fn (mut p Parser) parse_ident(language table.Language) ast.Ident {
|
||||
mut name := p.check_name()
|
||||
if name == '_' {
|
||||
return ast.Ident{
|
||||
tok_kind: p.tok.kind
|
||||
name: '_'
|
||||
kind: .blank_ident
|
||||
pos: pos
|
||||
@ -866,6 +867,7 @@ pub fn (mut p Parser) parse_ident(language table.Language) ast.Ident {
|
||||
name = '${p.expr_mod}.$name'
|
||||
}
|
||||
return ast.Ident{
|
||||
tok_kind: p.tok.kind
|
||||
kind: .unresolved
|
||||
name: name
|
||||
language: language
|
||||
|
Loading…
Reference in New Issue
Block a user