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 <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
Luan Santos 2016-04-13 08:10:41 -07:00 committed by Rob Pike
parent 2c9d773f74
commit 8d8feb4d2f
2 changed files with 10 additions and 4 deletions

View 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 {

View 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.