Skip to content

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

Closed

Description

@phenpessoa

Hello!

I've seen two similar issues: #1289 and #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

Activity

  1. added
    needs-triage
    on Jun 27, 2022
  2. removed
    needs-triage
    on Jun 27, 2022
  3. dominikh commented on Jun 27, 2022

    @dominikh
    Owner

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

  4. Rory-Z commented on Dec 12, 2022

    @Rory-Z

    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?

  5. dominikh commented on Dec 12, 2022

    @dominikh
    Owner

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

  6. Rory-Z commented on Dec 12, 2022

    @Rory-Z

    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.

  7. mvdan commented on Dec 13, 2022

    @mvdan
    Contributor

    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.

  8. Rory-Z commented on Dec 14, 2022

    @Rory-Z
  9. dominikh commented on Jan 1, 2023

    @dominikh
  10. Rory-Z commented on Jan 3, 2023

    @Rory-Z
  11. qfornaguera commented on Nov 2, 2023

    @qfornaguera

    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 the unexported (build -> Build) make the lint disappear

  12. phonovision commented on Apr 19, 2024

    @phonovision

    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.

  13. dominikh commented on May 31, 2026

    @dominikh
    Owner

    I cannot reproduce the problem anymore, so it has probably been fixed. If anyone disagrees, leave a comment with steps to reproduce and I'll reopen the issue.

  14. added 3 commits that reference this issue on Jun 7, 2026
    f34af32
    d623b60
    875903a
  15. added a commit that references this issue on Jun 23, 2026
    f0503f7
  16. 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