Skip to content

unused: warning about unexported struct that is used only as explicit generic parameter to function in another package #1282

@GottfriedHerold

Description

@GottfriedHerold

Hi the following code snippet gives a warning (type EmptyStruct is unused (U1000)go-staticcheck
View Problem), when it should not (probably related to #1250 ):

I have an internal(i.e. under /internal/utils.go) package utils with a function TypeOfType taking a generic parameter

package utils

import "reflect"

// TypeOfType[T]() returns the reflect.Type of T.
// As opposed to reflect.TypeOf, this works with a type parameter rather than
// a value and also works for interface types T
func TypeOfType[T any]() reflect.Type {
	var t *T
	return reflect.TypeOf(t).Elem()
}

Then in some other package I do:

package foo

import (
    "testing"
   "some/path/to/internal/utils"
)

func TestFoo(t *testing.T){
    type EmptyStruct struct{} // local definition
    _ = utils.TypeOfType[EmptyStruct]()
}

Staticcheck complains about EmptyStruct being unused, when it is clearly not. This persists even if the returned value of utils.TypeOfType is actually used (The example above is just simplified) or being defined at global scope, but unexported.
Note that the issue only happens if TypeOfType is defined in another package.

I use MS Code, version is

go: /snap/go/current/bin/go: go version go1.18.2 linux/amd64

gotests:	/home/gottfried/go/bin/gotests	(version: v1.6.0 built with go: go1.18)
gomodifytags:	/home/gottfried/go/bin/gomodifytags	(version: v1.16.0 built with go: go1.18)
impl:	/home/gottfried/go/bin/impl	(version: v1.1.0 built with go: go1.18)
goplay:	/home/gottfried/go/bin/goplay	(version: v1.0.0 built with go: go1.18)
dlv:	/home/gottfried/go/bin/dlv	(version: v1.8.2 built with go: go1.18)
staticcheck:	/home/gottfried/go/bin/staticcheck	(version: v0.3.0 built with go: go1.18)
gopls:	/home/gottfried/go/bin/gopls	(version: v0.8.4 built with go: go1.18.2)

Best,
Gottfried

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions