A Go static analysis tool that ensures only valid enum values are returned for enum types.
type StatusEnum string
const (
StatusActive StatusEnum = "active"
StatusInactive StatusEnum = "inactive"
)
func getStatus() StatusEnum {
return StatusActive // ✅ Valid - returns enum constant
}
var validStatus StatusEnum = StatusActive // ✅ Valid - variable assigned enum constanttype StatusEnum string
const (
StatusActive StatusEnum = "active"
StatusInactive StatusEnum = "inactive"
)
func getStatus() StatusEnum {
return "invalid" // ❌ Error - returns string literal
}
var invalidStatus StatusEnum = "random string" // ❌ Error - variable assigned string literal- Enum Detection: Automatically identifies types that have constants defined for them
- Multi-type Support: Works with string, int, float, bool, and iota-based enums
- Return Validation: Ensures only valid enum constants are returned from functions
- Variable Declaration Validation: Ensures only valid enum constants are assigned to variables
- Comprehensive Testing: Full test suite using Go's native testing framework
# Clone the repository
git clone <repository-url>
cd go_linter
# Build the linter
go build -o enumlinter cmd/main.go# Analyze a single file
./enumlinter path/to/file.go
# Analyze multiple files
./enumlinter file1.go file2.go
# Analyze a directory
./enumlinter ./path/to/directory./run_tests.sh# Run analyzer tests
cd pkg/analyzer && go test -v
# Run specific test
go test -v -run TestEnumLinter- String-based:
type Status string - Int-based:
type Priority int - Float-based:
type Score float64 - Bool-based:
type Flag bool - Iota-based:
type Color intwithiota
- Add test files to
testdata/ - Use
// want "expected error message"comments for invalid cases - Run tests with
go test -v
The core logic is in pkg/analyzer/analyzer.go. The analyzer:
- Detects enum types by finding types with constants
- Validates return statements against enum constants
- Validates variable declarations against enum constants
- Reports violations with clear error messages
MIT License