]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/vet: allow untyped composite literals to be unkeyed
authorLuan Santos <cfcluan@gmail.com>
Wed, 13 Apr 2016 15:10:41 +0000 (08:10 -0700)
committerRob Pike <r@golang.org>
Thu, 14 Apr 2016 16:20:58 +0000 (16:20 +0000)
We can trust that untyped composite literals are part of a slice literal
and not emit a vet warning for those.

Fixes #9171

Change-Id: Ia7c081e543b850f8be1fd1f9e711520061e70bed
Reviewed-on: https://go-review.googlesource.com/22000
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
src/cmd/vet/composite.go
src/cmd/vet/testdata/composite.go

index 731c793eac4d3eff6baeb0df2e91369c96c192d9..ac6a598b0b991db7669d00ecb9bec97ef0923297 100644 (file)
@@ -68,8 +68,10 @@ func checkUnkeyedLiteral(f *File, node ast.Node) {
        allKeyValue := true
        for _, e := range c.Elts {
                if _, ok := e.(*ast.KeyValueExpr); !ok {
-                       allKeyValue = false
-                       break
+                       if cl, ok := e.(*ast.CompositeLit); !ok || cl.Type != nil {
+                               allKeyValue = false
+                               break
+                       }
                }
        }
        if allKeyValue {
index 69e7d7ccb0a84e2a8a02e1a8aebaaeb752881e4d..0355c0b692ecabe43e4dc53d71f7e25ef6a984e1 100644 (file)
@@ -2,8 +2,6 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// This file contains tests for the untagged struct literal checker.
-
 // This file contains the test for untagged struct literals.
 
 package testdata
@@ -11,6 +9,7 @@ package testdata
 import (
        "flag"
        "go/scanner"
+       "unicode"
 )
 
 var Okay1 = []string{
@@ -57,6 +56,11 @@ var BadStructLiteralUsedInTests = flag.Flag{ // ERROR "unkeyed fields"
        "DefValue",
 }
 
+// SpecialCase is an (aptly named) slice of CaseRange to test issue 9171.
+var GoodNamedSliceLiteralUsedInTests = unicode.SpecialCase{
+       {Lo: 1, Hi: 2},
+}
+
 // Used to test the check for slices and arrays: If that test is disabled and
 // vet is run with --compositewhitelist=false, this line triggers an error.
 // Clumsy but sufficient.