Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive with generic interface and generic type #1616

Open
errrov opened this issue Nov 6, 2024 · 0 comments
Open

False positive with generic interface and generic type #1616

errrov opened this issue Nov 6, 2024 · 0 comments
Labels
false-positive needs-triage Newly filed issue that needs triage

Comments

@errrov
Copy link

errrov commented Nov 6, 2024

  • 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.

@errrov errrov added false-positive needs-triage Newly filed issue that needs triage labels Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
false-positive needs-triage Newly filed issue that needs triage
Projects
None yet
Development

No branches or pull requests

1 participant