From: Luan Santos Date: Wed, 13 Apr 2016 15:10:41 +0000 (-0700) Subject: cmd/vet: allow untyped composite literals to be unkeyed X-Git-Tag: go1.7beta1~687 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=8d8feb4d2fdb9c09821c4dc84e164e4c86d283ff;p=gostls13.git cmd/vet: allow untyped composite literals to be unkeyed 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 TryBot-Result: Gobot Gobot Reviewed-by: Rob Pike --- diff --git a/src/cmd/vet/composite.go b/src/cmd/vet/composite.go index 731c793eac..ac6a598b0b 100644 --- a/src/cmd/vet/composite.go +++ b/src/cmd/vet/composite.go @@ -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 { diff --git a/src/cmd/vet/testdata/composite.go b/src/cmd/vet/testdata/composite.go index 69e7d7ccb0..0355c0b692 100644 --- a/src/cmd/vet/testdata/composite.go +++ b/src/cmd/vet/testdata/composite.go @@ -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.