~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/cmd/vet/taglit.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
package main
8
8
 
9
9
import (
 
10
        "flag"
10
11
        "go/ast"
11
12
        "strings"
12
13
)
13
14
 
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")
 
16
 
 
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") {
 
21
                return
 
22
        }
 
23
 
 
24
        typ := c.Type
 
25
        for {
 
26
                if typ1, ok := c.Type.(*ast.ParenExpr); ok {
 
27
                        typ = typ1
 
28
                        continue
 
29
                }
 
30
                break
 
31
        }
 
32
 
 
33
        switch typ.(type) {
 
34
        case *ast.ArrayType:
 
35
                return
 
36
        case *ast.MapType:
 
37
                return
 
38
        case *ast.StructType:
 
39
                return // a literal struct type does not need to use tags
 
40
        case *ast.Ident:
 
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.)
 
46
                return
 
47
        }
 
48
 
 
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)
 
52
        if !isStruct {
 
53
                return
 
54
        }
 
55
 
 
56
        if typeString == "" { // isStruct doesn't know
 
57
                typeString = f.gofmt(typ)
 
58
        }
 
59
 
 
60
        // It's a struct, or we can't tell it's not a struct because we don't have types.
 
61
 
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)
41
86
        if path == "" {
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)
43
88
                return
44
89
        }
45
 
        typ := path + "." + s.Sel.Name
46
 
        if untaggedLiteralWhitelist[typ] {
 
90
        typeName := path + "." + s.Sel.Name
 
91
        if *compositeWhiteList && untaggedLiteralWhitelist[typeName] {
47
92
                return
48
93
        }
49
94
 
50
 
        f.Warnf(c.Pos(), "%s struct literal uses untagged fields", typ)
 
95
        f.Warn(c.Pos(), typeString+" composite literal uses untagged fields")
51
96
}
52
97
 
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,