go-tools
go-tools copied to clipboard
bug: generics: func(X).Y is unused(U1000)
Hello!
I've seen two similar issues: https://github.com/dominikh/go-tools/issues/1289 and https://github.com/dominikh/go-tools/issues/1282
But as they are not quite the same as this one, I think I'd open an issue about it, to make sure it is known for the unused rewrite.
Code:
type Foo[T any] interface {
bar() T
}
type FooBar struct{}
func (f *FooBar) bar() string {
return "foobar"
}
This will yield: func (*FooBar).bar is unused (U1000)
The same code without generics won't show any lint warnings, i.e:
type Foo interface {
bar() string
}
type FooBar struct{}
func (f *FooBar) bar() string {
return "foobar"
}
Staticcheck version: 2022.1.2 (v0.3.2) Go version: 1.18.3
Thanks, I'll add it to the test cases.
Any update? I'm using 0.3.3 version in go 1.19.4, and this issue is still exist, but my colleagues don't have this problem and they work well, did I miss something?
No, we didn't fix this yet. I don't know why your colleagues wouldn't have the problem.
No, we didn't fix this yet. I don't know why your colleagues wouldn't have the problem.
Thank‘s for your answer, I'm not sure why my colleagues wouldn't have the problem, we're working remotely, maybe they got it wrong somewhere, anyway, I've ignored "unused" in GitHub workflow for now, and looking forward to your fix, thank's again.
Raising my hand as well: it's only recently that I've started encountering generics in codebases at work, so I'm starting to see a few unused false positives as well. I think they are all caused by this bug, because the methods being marked as unused then mark other top-level funcs and types as unused as well.
No, we didn't fix this yet. I don't know why your colleagues wouldn't have the problem.
Do we have a roadmap for when to fix it?
Do we have a roadmap for when to fix it?
I'm working on it.
I'm working on it.
Great, I'm looking forward
Hello there!
I'm dropping here a case I've wrongly posted in golangci-lint repository. Maybe it helps
type builder[B any] interface {
// WithSomething mocks build setting.
WithSomething() B
build() struct{}
}
type baseBuilder[B any] struct {
builder B
}
func newBaseBuilder[B any](b B) *baseBuilder[B] {
return &baseBuilder[B]{
builder: b,
}
}
func (b *baseBuilder[B]) WithSomething() B {
return b.builder
}
func (b *baseBuilder[B]) build() struct{} {
return struct{}{}
}
// SomeBuilder is a builder for Some.
type SomeBuilder interface {
builder[SomeBuilder]
Build() struct{}
}
type someBuilder struct {
builder[SomeBuilder]
}
// NewSomeBuilder creates a new SomeBuilder.
func NewSomeBuilder() SomeBuilder {
b := &someBuilder{}
b.builder = newBaseBuilder[SomeBuilder](b)
return b
}
func (s *someBuilder) Build() struct{} {
return s.builder.build()
}
Removing WithSomething
from the interface or changing to unexported (build
-> Build
) make the lint disappear
I also tripped over this bug. It was a bit annoying that a simple nolint
above the affected methods didn't suffice: Subsequently to the seemingly unused method, a lot of other dependencies (like referenced constants and struct members) were also marked as unused.
My workaround was to export the method while keeping the interface unexported. For my use case, this was a feasible tradeoff.