Skip to content

False positive with generic interface and generic type #1616

Closed

Description

@errrov
  • The output of 'staticcheck -version' :
    staticcheck.exe 2024.1.1 (0.5.1)

  • The output of 'go version'
    go version go1.23.0 windows/amd64

  • Exactly which command you ran
    staticcheck main.go

  • Output of the command and what's wrong with the output

main.go:31:2: field v is unused (U1000)
main.go:38:28: func (*genericStruct[T]).setValue is unused (U1000)
main.go:42:28: func (*genericStruct[T]).getValue is unused (U1000)
package main

import (
	"fmt"
	"reflect"
)

type valueHandler[T any] interface {
	setValue(T)
	getValue() T
}

type handler[T any] struct {
	valueHandler[T]
}

func (h *handler[T]) setAndGetValue(v T) {
	h.setValue(v)
	va := h.getValue()
	fmt.Println("Value is : ", va)
}

type genericStruct[T any] struct {
	v T
}

func newGenericStruct[T any]() *genericStruct[T] {
	return &genericStruct[T]{}
}

func (g *genericStruct[T]) setValue(v T) {
	fmt.Println("setting value")
	g.v = v
}

func (g *genericStruct[T]) getValue() T {
	fmt.Println("getting value")
	return g.v
}

func main() {
	test := handler[int]{newGenericStruct[int]()}
	test.setAndGetValue(1)
}

So staticcheck shows that functions setValue and getValue are not used. However we can see that messages of "setting value" and "getting value" appear. So they are clearly used. For me it seems like false positive.

Activity

  1. added
    needs-triage
    on Nov 6, 2024
  2. rogpeppe commented on Nov 14, 2025

    @rogpeppe

    I came here to submit a similar example. If the thing that staticcheck says is unused really was unused, then the program would still compile if it was removed, but it is not.

    package somepkg
    
    type wrapper[A any] interface {
    	call(a *A)
    }
    
    func Foo[A any, T any](set func(*A, T)) {
    	w := wrapper[A](&impl[A, T]{set: set})
    	w.call(nil)
    }
    
    type impl[A, T any] struct {
    	thisIsUsed T
    	set        func(*A, T)
    }
    
    func (w *impl[A, T]) call(a *A) {
    	w.set(nil, w.thisIsUsed)
    }
    
  3. meling commented on Jan 26, 2026

    @meling

    I, too, was hit by this after a recent refactor that added a generic type parameter to the NodeListOption interface below.

    % staticcheck
    config_opts.go:25:30: func structNodeMap[K, V].newConfig is unused (U1000)
    config_opts.go:128:21: func nodeIDs[T].newConfig is unused (U1000)
    config_opts.go:160:22: func addNodes[T].newConfig is unused (U1000)
    config_opts.go:182:23: func addConfig[T].newConfig is unused (U1000)
    type NodeListOption[T NodeID] interface {
    	Option
    	newConfig(*Manager[T]) (Configuration[T], error)
    }
    
    type NodeAddress interface {
    	Addr() string
    }
    
    type structNodeMap[K NodeID, V NodeAddress] struct {
    	nodes map[K]V
    }
    
    func (structNodeMap[K, V]) isOption() {}
    
    func (o structNodeMap[K, V]) newConfig(mgr *Manager[K]) (nodes Configuration[K], err error) {
     ...
    }
    
    func WithNodes[K NodeID, V NodeAddress](nodes map[K]V) NodeListOption[K] {
    	return &structNodeMap[K, V]{nodes: nodes}
    }
    
    // The newConfig is used like this:
    
    func NewConfiguration[T NodeID](mgr *Manager[T], opt NodeListOption[T]) (nodes Configuration[T], err error) {
    	return opt.newConfig(mgr)
    }
  4. removed
    needs-triage
    on Feb 25, 2026
  5. added 3 commits that reference this issue on Jun 7, 2026
    f34af32
    d623b60
    875903a
  6. added a commit that references this issue on Jun 23, 2026
    f0503f7
  7. added a commit that references this issue on Jun 25, 2026
    9fce5f8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions