Home | History | Annotate | Download | only in testdata
      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 vardecl
      6 
      7 // Prerequisites.
      8 import "math"
      9 func f() {}
     10 func g() (x, y int) { return }
     11 var m map[string]int
     12 
     13 // Var decls must have a type or an initializer.
     14 var _ int
     15 var _, _ int
     16 
     17 // The first error message is produced by the parser.
     18 // In a real-world scenario, the type-checker would not be run
     19 // in this case and the 2nd error message would not appear.
     20 var _ /* ERROR "missing variable type" */ /* ERROR "missing type or init expr" */
     21 var _ /* ERROR "missing variable type" */ /* ERROR "missing type or init expr" */, _
     22 var _ /* ERROR "missing variable type" */ /* ERROR "missing type or init expr" */, _, _
     23 
     24 // The initializer must be an expression.
     25 var _ = int /* ERROR "not an expression" */
     26 var _ = f /* ERROR "used as value" */ ()
     27 
     28 // Identifier and expression arity must match.
     29 var _, _ = 1, 2
     30 var _ = 1, 2 /* ERROR "extra init expr 2" */
     31 var _, _ = 1 /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */
     32 var _, _, _ /* ERROR "missing init expr for _" */ = 1, 2
     33 
     34 var _ = g /* ERROR "2-valued g" */ ()
     35 var _, _ = g()
     36 var _, _, _ = g /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */ ()
     37 
     38 var _ = m["foo"]
     39 var _, _ = m["foo"]
     40 var _, _, _ = m  /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */ ["foo"]
     41 
     42 var _, _ int = 1, 2
     43 var _ int = 1, 2 /* ERROR "extra init expr 2" */
     44 var _, _ int = 1 /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */
     45 var _, _, _ /* ERROR "missing init expr for _" */ int = 1, 2
     46 
     47 var (
     48 	_, _ = 1, 2
     49 	_ = 1, 2 /* ERROR "extra init expr 2" */
     50 	_, _ = 1 /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */
     51 	_, _, _ /* ERROR "missing init expr for _" */ = 1, 2
     52 
     53 	_ = g /* ERROR "2-valued g" */ ()
     54 	_, _ = g()
     55 	_, _, _ = g /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */ ()
     56 
     57 	_ = m["foo"]
     58 	_, _ = m["foo"]
     59 	_, _, _ = m /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */ ["foo"]
     60 
     61 	_, _ int = 1, 2
     62 	_ int = 1, 2 /* ERROR "extra init expr 2" */
     63 	_, _ int = 1 /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */
     64 	_, _, _ /* ERROR "missing init expr for _" */ int = 1, 2
     65 )
     66 
     67 // Variables declared in function bodies must be 'used'.
     68 type T struct{}
     69 func (r T) _(a, b, c int) (u, v, w int) {
     70 	var x1 /* ERROR "declared but not used" */ int
     71 	var x2 /* ERROR "declared but not used" */ int
     72 	x1 = 1
     73 	(x2) = 2
     74 
     75 	y1 /* ERROR "declared but not used" */ := 1
     76 	y2 /* ERROR "declared but not used" */ := 2
     77 	y1 = 1
     78 	(y1) = 2
     79 
     80 	{
     81 		var x1 /* ERROR "declared but not used" */ int
     82 		var x2 /* ERROR "declared but not used" */ int
     83 		x1 = 1
     84 		(x2) = 2
     85 
     86 		y1 /* ERROR "declared but not used" */ := 1
     87 		y2 /* ERROR "declared but not used" */ := 2
     88 		y1 = 1
     89 		(y1) = 2
     90 	}
     91 
     92 	if x /* ERROR "declared but not used" */ := 0; a < b {}
     93 
     94 	switch x /* ERROR "declared but not used" */, y := 0, 1; a {
     95 	case 0:
     96 		_ = y
     97 	case 1:
     98 		x /* ERROR "declared but not used" */ := 0
     99 	}
    100 
    101 	var t interface{}
    102 	switch t /* ERROR "declared but not used" */ := t.(type) {}
    103 
    104 	switch t /* ERROR "declared but not used" */ := t.(type) {
    105 	case int:
    106 	}
    107 
    108 	switch t /* ERROR "declared but not used" */ := t.(type) {
    109 	case int:
    110 	case float32, complex64:
    111 		t = nil
    112 	}
    113 
    114 	switch t := t.(type) {
    115 	case int:
    116 	case float32, complex64:
    117 		_ = t
    118 	}
    119 
    120 	switch t := t.(type) {
    121 	case int:
    122 	case float32:
    123 	case string:
    124 		_ = func() string {
    125 			return t
    126 		}
    127 	}
    128 
    129 	switch t := t; t /* ERROR "declared but not used" */ := t.(type) {}
    130 
    131 	var z1 /* ERROR "declared but not used" */ int
    132 	var z2 int
    133 	_ = func(a, b, c int) (u, v, w int) {
    134 		z1 = a
    135 		(z1) = b
    136 		a = z2
    137 		return
    138 	}
    139 
    140 	var s []int
    141 	var i /* ERROR "declared but not used" */ , j int
    142 	for i, j = range s {
    143 		_ = j
    144 	}
    145 
    146 	for i, j /* ERROR "declared but not used" */ := range s {
    147 		_ = func() int {
    148 			return i
    149 		}
    150 	}
    151 	return
    152 }
    153 
    154 // Unused variables in closures must lead to only one error (issue #22524).
    155 func _() {
    156 	_ = func() {
    157 		var x /* ERROR declared but not used */ int
    158 	}
    159 }
    160 
    161 // Invalid (unused) expressions must not lead to spurious "declared but not used errors"
    162 func _() {
    163 	var a, b, c int
    164 	var x, y int
    165 	x, y = a /* ERROR cannot assign [0-9]+ values to [0-9]+ variables */ , b, c
    166 	_ = x
    167 	_ = y
    168 }
    169 
    170 func _() {
    171 	var x int
    172 	return x /* ERROR no result values expected */
    173 	return math /* ERROR no result values expected */ .Sin(0)
    174 }
    175 
    176 func _() int {
    177 	var x, y int
    178 	return /* ERROR wrong number of return values */ x, y
    179 }
    180 
    181 // Short variable declarations must declare at least one new non-blank variable.
    182 func _() {
    183 	_ := /* ERROR no new variables */ 0
    184 	_, a := 0, 1
    185 	_, a := /* ERROR no new variables */ 0, 1
    186 	_, a, b := 0, 1, 2
    187 	_, _, _ := /* ERROR no new variables */ 0, 1, 2
    188 
    189 	_ = a
    190 	_ = b
    191 }
    192 
    193 // TODO(gri) consolidate other var decl checks in this file