14
// checkUntaggedLiteral checks if a composite literal is an struct literal with
15
var compositeWhiteList = flag.Bool("compositewhitelist", true, "use composite white list; for testing only")
17
// checkUntaggedLiteral checks if a composite literal is a struct literal with
15
18
// untagged fields.
16
19
func (f *File) checkUntaggedLiteral(c *ast.CompositeLit) {
20
if !vet("composites") {
26
if typ1, ok := c.Type.(*ast.ParenExpr); ok {
39
return // a literal struct type does not need to use tags
41
// A simple type name like t or T does not need tags either,
42
// since it is almost certainly declared in the current package.
43
// (The exception is names being used via import . "pkg", but
44
// those are already breaking the Go 1 compatibility promise,
45
// so not reporting potential additional breakage seems okay.)
49
// Otherwise the type is a selector like pkg.Name.
50
// We only care if pkg.Name is a struct, not if it's a map, array, or slice.
51
isStruct, typeString := f.pkg.isStruct(c)
56
if typeString == "" { // isStruct doesn't know
57
typeString = f.gofmt(typ)
60
// It's a struct, or we can't tell it's not a struct because we don't have types.
17
62
// Check if the CompositeLit contains an untagged field.
18
63
allKeyValue := true
19
64
for _, e := range c.Elts {
39
84
// Convert the package name to an import path, and compare to a whitelist.
40
85
path := pkgPath(f, pkg.Name)
42
f.Warnf(c.Pos(), "unresolvable package for %s.%s literal", pkg.Name, s.Sel.Name)
87
f.Badf(c.Pos(), "unresolvable package for %s.%s literal", pkg.Name, s.Sel.Name)
45
typ := path + "." + s.Sel.Name
46
if untaggedLiteralWhitelist[typ] {
90
typeName := path + "." + s.Sel.Name
91
if *compositeWhiteList && untaggedLiteralWhitelist[typeName] {
50
f.Warnf(c.Pos(), "%s struct literal uses untagged fields", typ)
95
f.Warn(c.Pos(), typeString+" composite literal uses untagged fields")
53
98
// pkgPath returns the import path "image/png" for the package name "png".
94
139
"encoding/xml.CharData": true,
95
140
"encoding/xml.Comment": true,
96
141
"encoding/xml.Directive": true,
97
"exp/norm.Decomposition": true,
98
"exp/types.ObjList": true,
99
142
"go/scanner.ErrorList": true,
100
143
"image/color.Palette": true,
101
144
"net.HardwareAddr": true,