go-tools icon indicating copy to clipboard operation
go-tools copied to clipboard

bug: generics: func(X).Y is unused(U1000)

Open phenpessoa opened this issue 2 years ago • 10 comments

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

phenpessoa avatar Jun 27 '22 14:06 phenpessoa

Thanks, I'll add it to the test cases.

dominikh avatar Jun 27 '22 17:06 dominikh

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?

Rory-Z avatar Dec 12 '22 06:12 Rory-Z

No, we didn't fix this yet. I don't know why your colleagues wouldn't have the problem.

dominikh avatar Dec 12 '22 13:12 dominikh

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.

Rory-Z avatar Dec 12 '22 15:12 Rory-Z

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.

mvdan avatar Dec 13 '22 21:12 mvdan

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?

Rory-Z avatar Dec 14 '22 01:12 Rory-Z

Do we have a roadmap for when to fix it?

I'm working on it.

dominikh avatar Jan 01 '23 15:01 dominikh

I'm working on it.

Great, I'm looking forward

Rory-Z avatar Jan 03 '23 01:01 Rory-Z

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

qfornaguera avatar Nov 02 '23 11:11 qfornaguera

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.

phonovision avatar Apr 19 '24 10:04 phonovision