github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/types/testdata/check/errors.go (about)

     1  // Copyright 2013 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  package errors
     6  
     7  // Testing precise operand formatting in error messages
     8  // (matching messages are regular expressions, hence the \'s).
     9  func f(x int, m map[string]int) {
    10  	// no values
    11  	_ = f /* ERROR "f(0, m) (no value) used as value" */ (0, m)
    12  
    13  	// built-ins
    14  	_ = println // ERROR "println (built-in) must be called"
    15  
    16  	// types
    17  	_ = complex128 // ERROR "complex128 (type) is not an expression"
    18  
    19  	// constants
    20  	const c1 = 991
    21  	const c2 float32 = 0.5
    22  	const c3 = "foo"
    23  	0 // ERROR "0 (untyped int constant) is not used"
    24  	0.5 // ERROR "0.5 (untyped float constant) is not used"
    25  	"foo" // ERROR `"foo" (untyped string constant) is not used`
    26  	c1 // ERROR "c1 (untyped int constant 991) is not used"
    27  	c2 // ERROR "c2 (constant 0.5 of type float32) is not used"
    28  	c1 /* ERROR "c1 + c2 (constant 991.5 of type float32) is not used" */ + c2
    29  	c3 // ERROR `c3 (untyped string constant "foo") is not used`
    30  
    31  	// variables
    32  	x // ERROR "x (variable of type int) is not used"
    33  
    34  	// values
    35  	nil // ERROR "nil is not used"
    36  	( /* ERROR "(*int)(nil) (value of type *int) is not used" */ *int)(nil)
    37  	x /* ERROR "x != x (untyped bool value) is not used" */ != x
    38  	x /* ERROR "x + x (value of type int) is not used" */ + x
    39  
    40  	// value, ok's
    41  	const s = "foo"
    42  	m /* ERROR "m[s] (map index expression of type int) is not used" */ [s]
    43  }
    44  
    45  // Valid ERROR comments can have a variety of forms.
    46  func _() {
    47  	0 /* ERRORx "0 .* is not used" */
    48  	0 /* ERRORx "0 .* is not used" */
    49  	0 // ERRORx "0 .* is not used"
    50  	0 // ERRORx "0 .* is not used"
    51  }
    52  
    53  // Don't report spurious errors as a consequence of earlier errors.
    54  // Add more tests as needed.
    55  func _() {
    56  	if err := foo /* ERROR "undefined" */ (); err != nil /* "no error here" */ {}
    57  }
    58  
    59  // Use unqualified names for package-local objects.
    60  type T struct{}
    61  var _ int = T /* ERROR "value of type T" */ {} // use T in error message rather than errors.T
    62  
    63  // Don't report errors containing "invalid type" (issue #24182).
    64  func _(x *missing /* ERROR "undefined: missing" */ ) {
    65  	x.m() // there shouldn't be an error here referring to *invalid type
    66  }