| Basic test of unusedfunc. |
| |
| -- go.mod -- |
| module example.com |
| |
| go 1.21 |
| |
| -- a/a.go -- |
| package a |
| |
| func main() { |
| _ = live |
| } |
| |
| // -- functions -- |
| |
| func Exported() {} |
| |
| func dead() { // want `function "dead" is unused` |
| } |
| |
| func deadRecursive() int { // want `function "deadRecursive" is unused` |
| return deadRecursive() |
| } |
| |
| func live() {} |
| |
| //go:linkname foo |
| func apparentlyDeadButHasPrecedingLinknameComment() {} |
| |
| // -- methods -- |
| |
| type ExportedType int |
| type unexportedType int |
| |
| func (ExportedType) Exported() {} |
| func (unexportedType) Exported() {} |
| |
| func (x ExportedType) dead() { // want `method "dead" is unused` |
| x.dead() |
| } |
| |
| func (u unexportedType) dead() { // want `method "dead" is unused` |
| u.dead() |
| } |
| |
| func (x ExportedType) dynamic() {} // matches name of interface method => live |
| |
| type _ interface{ dynamic() } |
| |
| // -- types without methods -- |
| |
| type ExportedType2 int |
| |
| // self-references don't count |
| type unusedUnexportedType2 struct{ *unusedUnexportedType2 } // want `type "unusedUnexportedType2" is unused` |
| |
| type ( |
| one int |
| two one // want `type "two" is unused` |
| ) |
| |
| // -- generic methods -- |
| |
| type g[T any] int |
| |
| func (g[T]) method() {} // want `method "method" is unused` |
| |
| // -- constants -- |
| |
| const unusedConst = 1 // want `const "unusedConst" is unused` |
| |
| const ( |
| unusedEnum = iota // want `const "unusedEnum" is unused` |
| ) |
| |
| const constOne = 1 |
| const unusedConstTwo = constOne // want `const "unusedConstTwo" is unused` |
| const _, unusedConstThree = 0, 3 // want `const "unusedConstThree" is unused` |
| const unusedConstFour, _ = 4, 0 // want `const "unusedConstFour" is unused` |
| const unusedConstFive, UsedConst6 = 4, 6 // want `const "unusedConstFive" is unused` |
| |
| // This test verifies the fix for golang/go#76924. |
| // We no longer produce unused variable warnings for a group |
| // of unused constants if all constants have the same type and |
| // at least one is used. |
| |
| const ( |
| a = "a" |
| b = "b" |
| c = "c" |
| ) |
| |
| func _() { |
| print(a) |
| } |
| |
| const ( // want `all values in this set of constants are unused` |
| d = "d" |
| e = "e" |
| ) |
| |
| // -- vars -- |
| |
| var unusedVar = 1 // want `var "unusedVar" is unused` |
| |
| var ( |
| unusedVar2 = 1 // want `var "unusedVar2" is unused` |
| ) |
| |
| var ( |
| usedVar int |
| unusedVar3 = usedVar // want `var "unusedVar3" is unused` |
| _, unusedVar4, _ = Triple() // want `var "unusedVar4" is unused` |
| ) |
| |
| func Triple() (int, int, int) |
| |
| -- a/a.go.golden -- |
| package a |
| |
| func main() { |
| _ = live |
| } |
| |
| // -- functions -- |
| |
| func Exported() {} |
| |
| func live() {} |
| |
| //go:linkname foo |
| func apparentlyDeadButHasPrecedingLinknameComment() {} |
| |
| // -- methods -- |
| |
| type ExportedType int |
| type unexportedType int |
| |
| func (ExportedType) Exported() {} |
| func (unexportedType) Exported() {} |
| |
| func (x ExportedType) dynamic() {} // matches name of interface method => live |
| |
| type _ interface{ dynamic() } |
| |
| |
| // -- types without methods -- |
| |
| type ExportedType2 int |
| |
| type ( |
| one int |
| ) |
| |
| // -- generic methods -- |
| |
| type g[T any] int |
| |
| // -- constants -- |
| |
| const constOne = 1 |
| |
| const UsedConst6 = 6 // want `const "unusedConstFive" is unused` |
| |
| // This test verifies the fix for golang/go#76924. |
| // We no longer produce unused variable warnings for a group |
| // of unused constants if all constants have the same type and |
| // at least one is used. |
| |
| const ( |
| a = "a" |
| b = "b" |
| c = "c" |
| ) |
| |
| func _() { |
| print(a) |
| } |
| |
| // -- vars -- |
| |
| var ( |
| usedVar int |
| _, _, _ = Triple() // want `var "unusedVar4" is unused` |
| ) |
| |
| func Triple() (int, int, int) |