checker: suggest using `:=` when assigning to undefined variable (#5911)
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, '...')`
|
||||
|
|
|
@ -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 | }
|
|
@ -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