Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/loov/enumcheck
Allows to mark Go enum types as exhaustive.
https://github.com/loov/enumcheck
go golang static-analysis
Last synced: about 1 month ago
JSON representation
Allows to mark Go enum types as exhaustive.
- Host: GitHub
- URL: https://github.com/loov/enumcheck
- Owner: loov
- License: mit
- Created: 2019-07-26T15:59:56.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-09-04T14:01:14.000Z (4 months ago)
- Last Synced: 2024-11-08T21:55:12.665Z (about 1 month ago)
- Topics: go, golang, static-analysis
- Language: Go
- Homepage:
- Size: 47.9 KB
- Stars: 33
- Watchers: 4
- Forks: 2
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# enumcheck
***This is still a WIP, so exact behavior may change.***
Analyzer for exhaustive enum switches.
To install:
```
go install loov.dev/enumcheck@latest
```This package reports errors for:
``` go
//enumcheck:exhaustive
type Letter byteconst (
Alpha Letter = iota
Beta
Gamma
)func Switch(x Letter) {
switch x { // error: "missing cases Beta, Gamma and default"
case Alpha:
fmt.Println("alpha")
case 4: // error: "implicit conversion of 4 to Letter"
fmt.Println("beta")
}
}func Assignment() {
var x Letter
x = 123 // error: "implicit conversion of 123 to Letter
}```
This can also be used with types:
``` go
//enumcheck:exhaustive
type Expr interface{}var _ Expr = Add{}
var _ Expr = Mul{}type Add []Expr
type Mul []Exprtype Invalid []Expr
func Switch(x Expr) {
switch x.(type) { // error: "missing cases Mul"
case Add:
fmt.Println("alpha")
case Invalid: // error: "implicit conversion of Invalid to Expr"
fmt.Println("beta")
default:
fmt.Println("unknown")
}
}func Assignment() {
var x Expr
x = 3 // error: "implicit conversion of 3 to Expr
_ = x
}
```Or with structs:
``` go
//enumcheck:exhaustive
type Option struct{ value string }var (
True = Option{"true"}
False = Option{"false"}
Maybe = Option{"maybe"}
)func DayNonExhaustive() {
var day Optionswitch day { // want "missing cases False, Maybe and default"
case Option{"invalid"}: // want "invalid enum for enumstruct.Option"
fmt.Println("beta")
case True:
fmt.Println("beta")
}
}
```Mode `//enumcheck:relaxed` allows to make "default" case optional:
``` go
//enumcheck:relaxed
type Option stringvar (
Alpha = Option("alpha")
Beta = Option("beta")
)func Relaxed() {
var day Option
switch day {
case Alpha:
fmt.Println("alpha")
case Beta:
fmt.Println("beta")
}
}
```Mode `//enumcheck:silent` allows to silence reports for switch statements:
``` go
//enumcheck:silent
type Option stringvar (
Alpha = Option("alpha")
Beta = Option("beta")
)func NoErrorHere() {
var day Option
switch day {
case Beta:
fmt.Println("beta")
}
}func EnablePerSwitch() {
var day Option
switch day { //enumcheck:exhaustive
case Beta:
fmt.Println("beta")
}
}
```