mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: add a suggestion for misspelled field names in struct literals
This commit is contained in:
@ -3,6 +3,7 @@
|
|||||||
module checker
|
module checker
|
||||||
|
|
||||||
import v.ast
|
import v.ast
|
||||||
|
import v.util
|
||||||
|
|
||||||
pub fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
|
pub fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
|
||||||
mut struct_sym, struct_typ_idx := c.table.find_sym_and_type_idx(node.name)
|
mut struct_sym, struct_typ_idx := c.table.find_sym_and_type_idx(node.name)
|
||||||
@ -391,7 +392,8 @@ pub fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type {
|
|||||||
ast.StructField{}
|
ast.StructField{}
|
||||||
}
|
}
|
||||||
if !exists {
|
if !exists {
|
||||||
c.error('unknown field `$field.name` in struct literal of type `$type_sym.name`',
|
existing_fields := c.table.struct_fields(type_sym).map(it.name)
|
||||||
|
c.error(util.new_suggestion(field.name, existing_fields).say('unknown field `$field.name` in struct literal of type `$type_sym.name`'),
|
||||||
field.pos)
|
field.pos)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
vlib/v/checker/tests/struct_unknown_field.vv:8:3: error: unknown field `bar` in struct literal of type `Test`
|
vlib/v/checker/tests/struct_unknown_field.vv:8:3: error: unknown field `bar` in struct literal of type `Test`.
|
||||||
|
1 possibility: `foo`.
|
||||||
6 | t := Test{
|
6 | t := Test{
|
||||||
7 | foo: true
|
7 | foo: true
|
||||||
8 | bar: false
|
8 | bar: false
|
||||||
|
8
vlib/v/checker/tests/unknown_field_in_struct_literal.out
Normal file
8
vlib/v/checker/tests/unknown_field_in_struct_literal.out
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
vlib/v/checker/tests/unknown_field_in_struct_literal.vv:10:2: error: unknown field `pos` in struct literal of type `TileLine`.
|
||||||
|
Did you mean `ypos`?
|
||||||
|
8 |
|
||||||
|
9 | hline := TileLine{
|
||||||
|
10 | pos: 123
|
||||||
|
| ~~~~~~~~
|
||||||
|
11 | }
|
||||||
|
12 | println(hline)
|
12
vlib/v/checker/tests/unknown_field_in_struct_literal.vv
Normal file
12
vlib/v/checker/tests/unknown_field_in_struct_literal.vv
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
struct TileLine {
|
||||||
|
ypos int
|
||||||
|
mut:
|
||||||
|
field [5]int
|
||||||
|
points int
|
||||||
|
shifts int
|
||||||
|
}
|
||||||
|
|
||||||
|
hline := TileLine{
|
||||||
|
pos: 123
|
||||||
|
}
|
||||||
|
println(hline)
|
Reference in New Issue
Block a user