GitHub - unused-interface-methods/unused-interface-methods: Static analyzer that hunts down unused interface methods in your Go codebase

Go Version Go Report Card coverage Last Commit Project Status

๐Ÿš€ Lightning-fast static analyzer that hunts down unused interface methods in your Go codebase

๐ŸŽฏ Overview

unused-interface-methods is a powerful static analysis tool for Go that detects interface methods that are declared but never used anywhere in your codebase. Built on top of golang.org/x/tools/go/analysis, it seamlessly integrates with your development workflow.

๐Ÿ’ก Why You Need This

  • ๐Ÿงน Clean APIs: Dead interface methods confuse users and bloat your public API
  • โšก Faster Builds: Removing unused code makes compilation faster
  • ๐Ÿ”ง Easier Refactoring: Less surface area = simpler maintenance
  • ๐Ÿšฆ CI-Ready: Non-zero exit status when issues are found

๐Ÿค” Problem Example

package some_name

// Partially implemented interface
type Interface interface {
    SomeMethod()
    UnusedMethod() // unused for SomeObject - can be removed
}

type SomeObject struct {
    i Interface
}

func (o *SomeObject) SomeMethod() {
    o.i.SomeMethod() // definitely used interface method
}

โœจ Features

  • ๐ŸŽฏ Smart Detection: Finds unused methods on ordinary and generic interfaces (Go 1.18+)
  • ๐Ÿง  Context-Aware: Understands complex usage patterns:
    • ๐Ÿ“Ž Method values & function pointers
    • ๐Ÿ”„ Type assertions & type switches
    • ๐Ÿ“ฆ Embedded interfaces (bidirectional)
    • ๐Ÿ–จ๏ธ fmt package implicit String() calls
  • ๐Ÿ“Š Clean Output: Sorted by file path and line numbers
  • ๐Ÿ”Œ Editor Integration: Works with go vet, gopls, and your favorite IDE
  • ๐ŸŒ Cross-Platform: Full support for Windows, Linux, and macOS

๐Ÿš€ Quick Start

# Install the tool globally
go install github.com/unused-interface-methods/unused-interface-methods@latest

unused-interface-methods ./...

โš™๏ธ Configuration

# unused-interface-methods.yml
ignore:
  - "**/*_test.go"
  - "test/**"
  - "**/*_mock.go"
  - "**/mock/**"
  - "**/mocks/**"

The configuration file is automatically searched in the current directory (or .config/) with an optional dot prefix.

๐Ÿ”ง VS Code Integration

Ctrl+Shift+P (Cmd+Shift+P on Mac) โ†’ "Tasks: Run Task" โ†’ "Go: Check Unused Interface Methods"

  • โœ… Real-time highlighting of unused interface methods
  • โœ… Problems panel integration with clickable errors
  • โœ… File explorer markers showing files with issues

๐Ÿ”„ Optional: Install the Trigger Task on Save extension to automatically run the task silently on file save.

๐Ÿ“‹ Sample Output

path/interfaces.go:41:2: method "OnError" of interface "EventHandler" is declared but not used
path/interfaces.go:42:2: method "Subscribe" of interface "EventHandler" is declared but not used
path/interfaces.go:43:2: method "UnSubscribe" of interface "EventHandler" is declared but not used

๐Ÿ’ก Pro Tip: Output format is identical to go vet - your editor will highlight issues automatically!

๐Ÿ”ง Integration with other analyzers

import (
    "golang.org/x/tools/go/analysis"
    "github.com/unused-interface-methods/unused-interface-methods"
)

// Add to your multichecker
analyzers := []*analysis.Analyzer{
    unusedInterfaceMethods.Analyzer,
    // ... other analyzers
}

๐Ÿ”จ Development

# Install golangci-lint
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest

# Clone and build
git clone https://github.com/unused-interface-methods/unused-interface-methods.git
cd unused-interface-methods
make build

โš ๏ธ Known Limitations

  • ๐Ÿชž Reflection: Cannot track reflect.Value.Call() usage
  • ๐Ÿ”Œ Plugins: Runtime plugin loading is not tracked
  • ๐Ÿงช Generics: Best-effort matching; edge cases may slip through

๐Ÿค Contributing

We โค๏ธ contributions! Please include:

  1. ๐Ÿ› Reproducer (code snippet or minimal repo)
  2. ๐Ÿ“Š Expected vs actual output
  3. ๐Ÿ”– Go version (go version)

๐Ÿ“ฌ PRs are welcome too.

โญ Star this repo if it helped you write cleaner Go code.