| 1 |
Basic test of unusedfunc. |
| 2 |
|
| 3 |
-- go.mod -- |
| 4 |
module example.com |
| 5 |
|
| 6 |
go 1.21 |
| 7 |
|
| 8 |
-- a/a.go -- |
| 9 |
package a |
| 10 |
|
| 11 |
func main() { |
| 12 |
_ = live |
| 13 |
} |
| 14 |
|
| 15 |
// -- functions -- |
| 16 |
|
| 17 |
func Exported() {} |
| 18 |
|
| 19 |
func dead() { // want `function "dead" is unused` |
| 20 |
} |
| 21 |
|
| 22 |
func deadRecursive() int { // want `function "deadRecursive" is unused` |
| 23 |
return deadRecursive() |
| 24 |
} |
| 25 |
|
| 26 |
func live() {} |
| 27 |
|
| 28 |
//go:linkname foo |
| 29 |
func apparentlyDeadButHasPrecedingLinknameComment() {} |
| 30 |
|
| 31 |
// -- methods -- |
| 32 |
|
| 33 |
type ExportedType int |
| 34 |
type unexportedType int |
| 35 |
|
| 36 |
func (ExportedType) Exported() {} |
| 37 |
func (unexportedType) Exported() {} |
| 38 |
|
| 39 |
func (x ExportedType) dead() { // want `method "dead" is unused` |
| 40 |
x.dead() |
| 41 |
} |
| 42 |
|
| 43 |
func (u unexportedType) dead() { // want `method "dead" is unused` |
| 44 |
u.dead() |
| 45 |
} |
| 46 |
|
| 47 |
func (x ExportedType) dynamic() {} // matches name of interface method => live |
| 48 |
|
| 49 |
type _ interface{ dynamic() } |
| 50 |
|
| 51 |
// -- types without methods -- |
| 52 |
|
| 53 |
type ExportedType2 int |
| 54 |
|
| 55 |
// self-references don't count |
| 56 |
type unusedUnexportedType2 struct{ *unusedUnexportedType2 } // want `type "unusedUnexportedType2" is unused` |
| 57 |
|
| 58 |
type ( |
| 59 |
one int |
| 60 |
two one // want `type "two" is unused` |
| 61 |
) |
| 62 |
|
| 63 |
// -- generic methods -- |
| 64 |
|
| 65 |
type g[T any] int |
| 66 |
|
| 67 |
func (g[T]) method() {} // want `method "method" is unused` |
| 68 |
|
| 69 |
// -- constants -- |
| 70 |
|
| 71 |
const unusedConst = 1 // want `const "unusedConst" is unused` |
| 72 |
|
| 73 |
const ( |
| 74 |
unusedEnum = iota // want `const "unusedEnum" is unused` |
| 75 |
) |
| 76 |
|
| 77 |
const constOne = 1 |
| 78 |
const unusedConstTwo = constOne // want `const "unusedConstTwo" is unused` |
| 79 |
const _, unusedConstThree = 0, 3 // want `const "unusedConstThree" is unused` |
| 80 |
const unusedConstFour, _ = 4, 0 // want `const "unusedConstFour" is unused` |
| 81 |
const unusedConstFive, UsedConst6 = 4, 6 // want `const "unusedConstFive" is unused` |
| 82 |
|
| 83 |
// This test verifies the fix for golang/go#76924. |
| 84 |
// We no longer produce unused variable warnings for a group |
| 85 |
// of unused constants if all constants have the same type and |
| 86 |
// at least one is used. |
| 87 |
|
| 88 |
const ( |
| 89 |
a = "a" |
| 90 |
b = "b" |
| 91 |
c = "c" |
| 92 |
) |
| 93 |
|
| 94 |
func _() { |
| 95 |
print(a) |
| 96 |
} |
| 97 |
|
| 98 |
const ( // want `all values in this set of constants are unused` |
| 99 |
d = "d" |
| 100 |
e = "e" |
| 101 |
) |
| 102 |
|
| 103 |
// -- vars -- |
| 104 |
|
| 105 |
var unusedVar = 1 // want `var "unusedVar" is unused` |
| 106 |
|
| 107 |
var ( |
| 108 |
unusedVar2 = 1 // want `var "unusedVar2" is unused` |
| 109 |
) |
| 110 |
|
| 111 |
var ( |
| 112 |
usedVar int |
| 113 |
unusedVar3 = usedVar // want `var "unusedVar3" is unused` |
| 114 |
_, unusedVar4, _ = Triple() // want `var "unusedVar4" is unused` |
| 115 |
) |
| 116 |
|
| 117 |
func Triple() (int, int, int) |
| 118 |
|
| 119 |
-- a/a.go.golden -- |
| 120 |
package a |
| 121 |
|
| 122 |
func main() { |
| 123 |
_ = live |
| 124 |
} |
| 125 |
|
| 126 |
// -- functions -- |
| 127 |
|
| 128 |
func Exported() {} |
| 129 |
|
| 130 |
func live() {} |
| 131 |
|
| 132 |
//go:linkname foo |
| 133 |
func apparentlyDeadButHasPrecedingLinknameComment() {} |
| 134 |
|
| 135 |
// -- methods -- |
| 136 |
|
| 137 |
type ExportedType int |
| 138 |
type unexportedType int |
| 139 |
|
| 140 |
func (ExportedType) Exported() {} |
| 141 |
func (unexportedType) Exported() {} |
| 142 |
|
| 143 |
func (x ExportedType) dynamic() {} // matches name of interface method => live |
| 144 |
|
| 145 |
type _ interface{ dynamic() } |
| 146 |
|
| 147 |
|
| 148 |
// -- types without methods -- |
| 149 |
|
| 150 |
type ExportedType2 int |
| 151 |
|
| 152 |
type ( |
| 153 |
one int |
| 154 |
) |
| 155 |
|
| 156 |
// -- generic methods -- |
| 157 |
|
| 158 |
type g[T any] int |
| 159 |
|
| 160 |
// -- constants -- |
| 161 |
|
| 162 |
const constOne = 1 |
| 163 |
|
| 164 |
const UsedConst6 = 6 // want `const "unusedConstFive" is unused` |
| 165 |
|
| 166 |
// This test verifies the fix for golang/go#76924. |
| 167 |
// We no longer produce unused variable warnings for a group |
| 168 |
// of unused constants if all constants have the same type and |
| 169 |
// at least one is used. |
| 170 |
|
| 171 |
const ( |
| 172 |
a = "a" |
| 173 |
b = "b" |
| 174 |
c = "c" |
| 175 |
) |
| 176 |
|
| 177 |
func _() { |
| 178 |
print(a) |
| 179 |
} |
| 180 |
|
| 181 |
// -- vars -- |
| 182 |
|
| 183 |
var ( |
| 184 |
usedVar int |
| 185 |
_, _, _ = Triple() // want `var "unusedVar4" is unused` |
| 186 |
) |
| 187 |
|
| 188 |
func Triple() (int, int, int) |