]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/vet: use type information in isLocalType
authorDaniel Martí <mvdan@mvdan.cc>
Wed, 21 Feb 2018 16:33:31 +0000 (16:33 +0000)
committerDaniel Martí <mvdan@mvdan.cc>
Wed, 25 Apr 2018 14:45:50 +0000 (14:45 +0000)
Now that vet always has type information, there's no reason to use
string handling on type names to gather information about them, such as
whether or not they are a local type.

The semantics remain the same - the only difference should be that the
implementation is less fragile and simpler.

Change-Id: I71386b4196922e4c9f2653d90abc382efbf01b3c
Reviewed-on: https://go-review.googlesource.com/95915
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
src/cmd/vet/composite.go

index fc4803318269a24a733b91961ae137b6cc6ff81a..965d73753ab3eadfbef9fa60d28b338277b452fa 100644 (file)
@@ -50,7 +50,7 @@ func checkUnkeyedLiteral(f *File, node ast.Node) {
                // skip non-struct composite literals
                return
        }
-       if isLocalType(f, typeName) {
+       if isLocalType(f, typ) {
                // allow unkeyed locally defined composite literal
                return
        }
@@ -71,24 +71,16 @@ func checkUnkeyedLiteral(f *File, node ast.Node) {
        f.Badf(cl.Pos(), "%s composite literal uses unkeyed fields", typeName)
 }
 
-func isLocalType(f *File, typeName string) bool {
-       if strings.HasPrefix(typeName, "struct{") {
+func isLocalType(f *File, typ types.Type) bool {
+       switch x := typ.(type) {
+       case *types.Struct:
                // struct literals are local types
                return true
+       case *types.Pointer:
+               return isLocalType(f, x.Elem())
+       case *types.Named:
+               // names in package foo are local to foo_test too
+               return strings.TrimSuffix(x.Obj().Pkg().Path(), "_test") == strings.TrimSuffix(f.pkg.path, "_test")
        }
-
-       // make *foo.bar, **foo.bar, etc match with the "foo." prefix
-       // below
-       typeName = strings.TrimLeft(typeName, "*")
-
-       pkgname := f.pkg.path
-       if strings.HasPrefix(typeName, pkgname+".") {
-               return true
-       }
-
-       // treat types as local inside test packages with _test name suffix
-       if strings.HasSuffix(pkgname, "_test") {
-               pkgname = pkgname[:len(pkgname)-len("_test")]
-       }
-       return strings.HasPrefix(typeName, pkgname+".")
+       return false
 }