dominikh / go-tools

Staticcheck - The advanced Go linter
https://staticcheck.dev
MIT License
6.21k stars 377 forks source link

False positive with generic interface and generic type #1616

Open errrov opened 2 weeks ago

errrov commented 2 weeks ago

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.