switch-check
is a tool for validating that switch
statements contain all
enum values.
go get -u github.com/elliotchance/switch-check
switch-check [options] [files or folders...]
-show-enums
Show all enums. Useful for debugging.
-verbose
Show all files processed.
package test
type Foo int
const (
FooA Foo = iota
FooB
)
const FooC = Foo(17)
const FooD = FooC
var FooE = Foo(-1)
func fooMissingSomeValues() {
foo := FooB
switch foo {
case FooA:
case FooC, FooD:
}
}
Run with switch-check
will output the error:
./test/foo.go:33:2 switch is missing cases for: FooB, FooE
- Using expressions to produce enum values are not supported. This level of type inference requires the compiler (not just the AST). For example this will not be recognised as a enum value:
var EnumValueA = someFuncThatReturnsAnEnumValue()
- Switch statements must only switch on the value and not contain expressions in case statements.