Skip to content

Repository files navigation

unused-interface-methods

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.

About

Static analyzer that hunts down unused interface methods in your Go codebase

Resources

Stars

6 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages