https://github.com/elliotchance/switch-check
Validate switch statements contain all enum values.
https://github.com/elliotchance/switch-check
enums golang static-analysis static-analyzer switch
Last synced: 10 months ago
JSON representation
Validate switch statements contain all enum values.
- Host: GitHub
- URL: https://github.com/elliotchance/switch-check
- Owner: elliotchance
- Created: 2020-05-03T19:08:33.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-09T16:10:26.000Z (about 6 years ago)
- Last Synced: 2025-03-25T01:51:07.138Z (over 1 year ago)
- Topics: enums, golang, static-analysis, static-analyzer, switch
- Language: Go
- Size: 30.3 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
switch-check
============
`switch-check` is a tool for validating that `switch` statements contain all
enum values.
Installation
------------
```bash
go get -u github.com/elliotchance/switch-check
```
Usage
-----
```bash
switch-check [options] [files or folders...]
-show-enums
Show all enums. Useful for debugging.
-verbose
Show all files processed.
```
Example
-------
```go
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
```
Known Limitations
-----------------
1. 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:
```go
var EnumValueA = someFuncThatReturnsAnEnumValue()
```
2. Switch statements must only switch on the value and not contain expressions
in case statements.