Skip to content

Is this unused false positive? #942

Closed

Description

@hiromoon

Hi, I have a question.
Is this a false positive?

Static checker warns unused function.

$ golangci-lint run
foo/example.go:15:16: func `(*Buzz).bar` is unused (unused)
func (b *Buzz) bar() {

This private function is used through interface.

package main

import "./foo"

func main() {
	foo.Bar(&foo.Buzz{})
}
package foo

import "fmt"

type Foo interface {
	bar()
}

func Bar(f Foo) {
	f.bar()
}

type Buzz struct{}

func (b *Buzz) bar() {
	fmt.Println("foo bar buzz")
}

This problem happens with golangci-lint@v1.38.0, not with golangci-lint@v1.37.1.

Activity

  1. dominikh commented on Mar 5, 2021

    @dominikh
    Owner

    Can reproduce, looking into it.

  2. added
    startedIssues we've started working on
    and removed
    needs-triageNewly filed issue that needs triage
    on Mar 5, 2021
  3. dominikh commented on Mar 5, 2021

    @dominikh
    Owner

    This should be fixed on master. I'd like to wait a day or two, to see if this change has caused any new bugs. If it hasn't, I'll make a new patch release.

  4. hiromoon commented on Mar 5, 2021

    @hiromoon
    Author

    I see! Thank you for quick response!

  5. added a commit that references this issue on Mar 9, 2021
    83b3bd3
  6. added a commit that references this issue on Apr 8, 2024
    9680b43
  7. ngotchac commented on Oct 21, 2025

    @ngotchac

    It seems to still be broken on some cases with generics. This reports bar is unused:

    package baz
    
    // Result is a generic type that represents the result of a function call
    type Result[T any] struct {
    	Value T
    	Error error
    }
    
    // Foo is an interface that defines the bar method
    type Foo[T any] interface {
    	bar() Result[T]
    }
    
    // Bar is a function that takes a Foo and calls its bar method
    func Bar[T any](f Foo[T]) Result[T] {
    	return f.bar()
    }
    
    // Buzz is a struct that implements the Foo interface
    type Buzz[T any] struct {
    	value T
    }
    
    func (b *Buzz[T]) bar() Result[T] {
    	return Result[T]{Value: b.value}
    }

    while

    package baz
    
    // Foo is an interface that defines the bar method
    type Foo[T any] interface {
    	bar() T
    }
    
    // Bar is a function that takes a Foo and calls its bar method
    func Bar[T any](f Foo[T]) T {
    	return f.bar()
    }
    
    // Buzz is a struct that implements the Foo interface
    type Buzz[T any] struct {
    	value T
    }
    
    func (b *Buzz[T]) bar() T {
    	return b.value
    }

    works fine (returns T instead of Result[T]).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions