Skip to content

Commit 7e20d40

Browse files
committed
go/types/typeutil: when flattening, don't ignore embedded types that aren't structs
Closes: gh-1443 (cherry picked from commit ad5ca31)
1 parent 0aaa8fa commit 7e20d40

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

go/types/typeutil/util.go

+2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ func flattenFields(T *types.Struct, path []int, seen map[types.Type]bool) []Fiel
122122
if field.Anonymous() {
123123
if s, ok := Dereference(field.Type()).Underlying().(*types.Struct); ok {
124124
out = append(out, flattenFields(s, np, seen)...)
125+
} else {
126+
out = append(out, Field{field, tag, np})
125127
}
126128
} else {
127129
out = append(out, Field{field, tag, np})

staticcheck/testdata/src/example.com/CheckNoopMarshal/CheckNoopMarshal.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ type T18 struct {
4242
Actual int
4343
}
4444

45+
type t19 string
46+
type T20 string
47+
48+
type T21 struct{ t19 }
49+
type T22 struct{ T20 }
50+
type T23 struct{ *T20 }
51+
4552
func fn() {
4653
// don't flag structs with no fields
4754
json.Marshal(T1{})
@@ -79,7 +86,14 @@ func fn() {
7986
json.Marshal(T17{}) //@ diag(`struct type 'example.com/CheckNoopMarshal.T17' doesn't have any exported fields, nor custom marshaling`)
8087
json.Marshal(T18{})
8188

82-
// MarshalJSON does not apply to JSON
89+
// embedding an unexported, non-struct type
90+
json.Marshal(T21{}) //@ diag(`struct type 'example.com/CheckNoopMarshal.T21' doesn't have any exported fields, nor custom marshaling`)
91+
// embedding an exported, non-struct type
92+
json.Marshal(T22{})
93+
// embedding an exported, non-struct type
94+
json.Marshal(T23{})
95+
96+
// MarshalJSON does not apply to XML
8397
xml.Marshal(T7{}) //@ diag(`struct type 'example.com/CheckNoopMarshal.T7' doesn't have any exported fields, nor custom marshaling`)
8498
// MarshalXML
8599
xml.Marshal(T8{})

0 commit comments

Comments
 (0)