Home | History | Annotate | Download | only in testdata
      1 // Copyright 2012 The Go Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style
      3 // license that can be found in the LICENSE file.
      4 
      5 // This file contains the test for untagged struct literals.
      6 
      7 package testdata
      8 
      9 import (
     10 	"flag"
     11 	"go/scanner"
     12 	"image"
     13 	"unicode"
     14 
     15 	"path/to/unknownpkg"
     16 )
     17 
     18 var Okay1 = []string{
     19 	"Name",
     20 	"Usage",
     21 	"DefValue",
     22 }
     23 
     24 var Okay2 = map[string]bool{
     25 	"Name":     true,
     26 	"Usage":    true,
     27 	"DefValue": true,
     28 }
     29 
     30 var Okay3 = struct {
     31 	X string
     32 	Y string
     33 	Z string
     34 }{
     35 	"Name",
     36 	"Usage",
     37 	"DefValue",
     38 }
     39 
     40 var Okay4 = []struct {
     41 	A int
     42 	B int
     43 }{
     44 	{1, 2},
     45 	{3, 4},
     46 }
     47 
     48 type MyStruct struct {
     49 	X string
     50 	Y string
     51 	Z string
     52 }
     53 
     54 var Okay5 = &MyStruct{
     55 	"Name",
     56 	"Usage",
     57 	"DefValue",
     58 }
     59 
     60 var Okay6 = []MyStruct{
     61 	{"foo", "bar", "baz"},
     62 	{"aa", "bb", "cc"},
     63 }
     64 
     65 // Testing is awkward because we need to reference things from a separate package
     66 // to trigger the warnings.
     67 
     68 var goodStructLiteral = flag.Flag{
     69 	Name:  "Name",
     70 	Usage: "Usage",
     71 }
     72 var badStructLiteral = flag.Flag{ // ERROR "unkeyed fields"
     73 	"Name",
     74 	"Usage",
     75 	nil, // Value
     76 	"DefValue",
     77 }
     78 
     79 // SpecialCase is a named slice of CaseRange to test issue 9171.
     80 var goodNamedSliceLiteral = unicode.SpecialCase{
     81 	{Lo: 1, Hi: 2},
     82 	unicode.CaseRange{Lo: 1, Hi: 2},
     83 }
     84 var badNamedSliceLiteral = unicode.SpecialCase{
     85 	{1, 2},                  // ERROR "unkeyed fields"
     86 	unicode.CaseRange{1, 2}, // ERROR "unkeyed fields"
     87 }
     88 
     89 // ErrorList is a named slice, so no warnings should be emitted.
     90 var goodScannerErrorList = scanner.ErrorList{
     91 	&scanner.Error{Msg: "foobar"},
     92 }
     93 var badScannerErrorList = scanner.ErrorList{
     94 	&scanner.Error{"foobar"}, // ERROR "unkeyed fields"
     95 }
     96 
     97 // Check whitelisted structs: if vet is run with --compositewhitelist=false,
     98 // this line triggers an error.
     99 var whitelistedPoint = image.Point{1, 2}
    100 
    101 // Do not check type from unknown package.
    102 // See issue 15408.
    103 var unknownPkgVar = unknownpkg.Foobar{"foo", "bar"}
    104