๐ 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)
- ๐จ๏ธ
fmtpackage implicitString()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:
- ๐ Reproducer (code snippet or minimal repo)
- ๐ Expected vs actual output
- ๐ Go version (
go version)
๐ฌ PRs are welcome too.
โญ Star this repo if it helped you write cleaner Go code.