mirror of
https://github.com/vlang/v.git
synced 2025-09-13 09:25:45 -04:00
checker: fix field ?&Type
without default value (#19786)
This commit is contained in:
parent
6fa9a84753
commit
09f3e1e971
@ -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
|
||||
|
21
vlib/v/checker/tests/option_ref_init_err.out
Normal file
21
vlib/v/checker/tests/option_ref_init_err.out
Normal file
@ -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 |
|
17
vlib/v/checker/tests/option_ref_init_err.vv
Normal file
17
vlib/v/checker/tests/option_ref_init_err.vv
Normal file
@ -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{}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user