...
Run Format

Source file src/cmd/vet/testdata/composite.go

Documentation: cmd/vet/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  var Okay7 = []*MyStruct{
    66  	{"foo", "bar", "baz"},
    67  	{"aa", "bb", "cc"},
    68  }
    69  
    70  // Testing is awkward because we need to reference things from a separate package
    71  // to trigger the warnings.
    72  
    73  var goodStructLiteral = flag.Flag{
    74  	Name:  "Name",
    75  	Usage: "Usage",
    76  }
    77  var badStructLiteral = flag.Flag{ // ERROR "unkeyed fields"
    78  	"Name",
    79  	"Usage",
    80  	nil, // Value
    81  	"DefValue",
    82  }
    83  
    84  // SpecialCase is a named slice of CaseRange to test issue 9171.
    85  var goodNamedSliceLiteral = unicode.SpecialCase{
    86  	{Lo: 1, Hi: 2},
    87  	unicode.CaseRange{Lo: 1, Hi: 2},
    88  }
    89  var badNamedSliceLiteral = unicode.SpecialCase{
    90  	{1, 2},                  // ERROR "unkeyed fields"
    91  	unicode.CaseRange{1, 2}, // ERROR "unkeyed fields"
    92  }
    93  
    94  // ErrorList is a named slice, so no warnings should be emitted.
    95  var goodScannerErrorList = scanner.ErrorList{
    96  	&scanner.Error{Msg: "foobar"},
    97  }
    98  var badScannerErrorList = scanner.ErrorList{
    99  	&scanner.Error{"foobar"}, // ERROR "unkeyed fields"
   100  }
   101  
   102  // Check whitelisted structs: if vet is run with --compositewhitelist=false,
   103  // this line triggers an error.
   104  var whitelistedPoint = image.Point{1, 2}
   105  
   106  // Do not check type from unknown package.
   107  // See issue 15408.
   108  var unknownPkgVar = unknownpkg.Foobar{"foo", "bar"}
   109  
   110  // A named pointer slice of CaseRange to test issue 23539. In
   111  // particular, we're interested in how some slice elements omit their
   112  // type.
   113  var goodNamedPointerSliceLiteral = []*unicode.CaseRange{
   114  	{Lo: 1, Hi: 2},
   115  	&unicode.CaseRange{Lo: 1, Hi: 2},
   116  }
   117  var badNamedPointerSliceLiteral = []*unicode.CaseRange{
   118  	{1, 2},                   // ERROR "unkeyed fields"
   119  	&unicode.CaseRange{1, 2}, // ERROR "unkeyed fields"
   120  }
   121  

View as plain text