Skip to content

Commit

Permalink
go/types/typeutil: when flattening, don't ignore embedded types that …
Browse files Browse the repository at this point in the history
…aren't structs

Closes: gh-1443
  • Loading branch information
dominikh committed Aug 26, 2023
1 parent 4670e26 commit ad5ca31
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions go/types/typeutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ func flattenFields(T *types.Struct, path []int, seen map[types.Type]bool) []Fiel
if field.Anonymous() {
if s, ok := Dereference(field.Type()).Underlying().(*types.Struct); ok {
out = append(out, flattenFields(s, np, seen)...)
} else {
out = append(out, Field{field, tag, np})
}
} else {
out = append(out, Field{field, tag, np})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ type T18 struct {
Actual int
}

type t19 string
type T20 string

type T21 struct{ t19 }
type T22 struct{ T20 }
type T23 struct{ *T20 }

func fn() {
// don't flag structs with no fields
json.Marshal(T1{})
Expand Down Expand Up @@ -79,7 +86,14 @@ func fn() {
json.Marshal(T17{}) //@ diag(`struct type 'example.com/CheckNoopMarshal.T17' doesn't have any exported fields, nor custom marshaling`)
json.Marshal(T18{})

// MarshalJSON does not apply to JSON
// embedding an unexported, non-struct type
json.Marshal(T21{}) //@ diag(`struct type 'example.com/CheckNoopMarshal.T21' doesn't have any exported fields, nor custom marshaling`)
// embedding an exported, non-struct type
json.Marshal(T22{})
// embedding an exported, non-struct type
json.Marshal(T23{})

// MarshalJSON does not apply to XML
xml.Marshal(T7{}) //@ diag(`struct type 'example.com/CheckNoopMarshal.T7' doesn't have any exported fields, nor custom marshaling`)
// MarshalXML
xml.Marshal(T8{})
Expand Down

0 comments on commit ad5ca31

Please sign in to comment.