Skip to content

Commit 42db392

Browse files
authored
checker: fix embedded structure initialization warnings (#18385)
1 parent dd1d5bc commit 42db392

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

vlib/v/checker/struct.v

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,16 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
586586
field.expr.pos)
587587
}
588588
}
589+
590+
// all the fields of initialized embedded struct are ignored, they are considered initialized
591+
sym := c.table.sym(field.typ)
592+
if field.name.len > 0 && field.name[0].is_capital() && sym.kind == .struct_
593+
&& sym.language == .v {
594+
struct_fields := c.table.struct_fields(sym)
595+
for struct_field in struct_fields {
596+
inited_fields << struct_field.name
597+
}
598+
}
589599
}
590600
// Check uninitialized refs/sum types
591601
// The variable `fields` contains two parts, the first part is the same as info.fields,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Outer{
2+
Embedded: Embedded{
3+
a: &nil
4+
}
5+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
struct Embedded {
2+
a &int
3+
}
4+
5+
fn new_embedded() Embedded {
6+
return Embedded{
7+
a: unsafe { nil }
8+
}
9+
}
10+
11+
struct Outer {
12+
Embedded
13+
}
14+
15+
fn main() {
16+
o := Outer{
17+
Embedded: new_embedded()
18+
}
19+
println(o)
20+
}

0 commit comments

Comments
 (0)