diff --git a/vlib/v/checker/struct.v b/vlib/v/checker/struct.v index b645a6dc135e64..02e12e19c4c36e 100644 --- a/vlib/v/checker/struct.v +++ b/vlib/v/checker/struct.v @@ -727,14 +727,16 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.', node.pos) continue } - if sym.kind == .struct_ { - c.check_ref_fields_initialized(sym, mut checked_types, '${type_sym.name}.${field.name}', - node) - } else if sym.kind == .alias { - parent_sym := c.table.sym((sym.info as ast.Alias).parent_type) - if parent_sym.kind == .struct_ { - c.check_ref_fields_initialized(parent_sym, mut checked_types, - '${type_sym.name}.${field.name}', node) + if !field.typ.has_flag(.option) { + if sym.kind == .struct_ { + c.check_ref_fields_initialized(sym, mut checked_types, '${type_sym.name}.${field.name}', + node) + } else if sym.kind == .alias { + parent_sym := c.table.sym((sym.info as ast.Alias).parent_type) + if parent_sym.kind == .struct_ { + c.check_ref_fields_initialized(parent_sym, mut checked_types, + '${type_sym.name}.${field.name}', node) + } } } // Do not allow empty uninitialized interfaces diff --git a/vlib/v/checker/tests/option_ref_init_err.out b/vlib/v/checker/tests/option_ref_init_err.out new file mode 100644 index 00000000000000..8c206781350516 --- /dev/null +++ b/vlib/v/checker/tests/option_ref_init_err.out @@ -0,0 +1,21 @@ +vlib/v/checker/tests/option_ref_init_err.vv:9:18: warning: unnecessary default value of `none`: struct fields are zeroed by default + 7 | mut: + 8 | a0 ?&Viewport + 9 | a1 ?&Viewport = none + | ~~~~ + 10 | a2 ?&Viewport = none + 11 | a3 ?&Viewport = unsafe { nil } +vlib/v/checker/tests/option_ref_init_err.vv:10:18: warning: unnecessary default value of `none`: struct fields are zeroed by default + 8 | a0 ?&Viewport + 9 | a1 ?&Viewport = none + 10 | a2 ?&Viewport = none + | ~~~~ + 11 | a3 ?&Viewport = unsafe { nil } + 12 | a4 ?&Viewport = nil +vlib/v/checker/tests/option_ref_init_err.vv:12:18: error: `nil` is only allowed in `unsafe` code + 10 | a2 ?&Viewport = none + 11 | a3 ?&Viewport = unsafe { nil } + 12 | a4 ?&Viewport = nil + | ~~~ + 13 | } + 14 | diff --git a/vlib/v/checker/tests/option_ref_init_err.vv b/vlib/v/checker/tests/option_ref_init_err.vv new file mode 100644 index 00000000000000..db288ac73523ff --- /dev/null +++ b/vlib/v/checker/tests/option_ref_init_err.vv @@ -0,0 +1,17 @@ +struct Viewport { +mut: + parent &Window +} + +pub struct Window { +mut: + a0 ?&Viewport + a1 ?&Viewport = none + a2 ?&Viewport = none + a3 ?&Viewport = unsafe { nil } + a4 ?&Viewport = nil +} + +fn main() { + _ := &Window{} +} \ No newline at end of file