2022-02-24 16:33:24 +00:00
|
|
|
## exhaustive [![Godoc][2]][1]
|
2021-11-14 20:01:54 +00:00
|
|
|
|
2022-02-24 16:33:24 +00:00
|
|
|
Check exhaustiveness of enum switch statements in Go source code.
|
2021-11-14 20:01:54 +00:00
|
|
|
|
|
|
|
```
|
2022-02-24 16:33:24 +00:00
|
|
|
go install github.com/nishanths/exhaustive/cmd/exhaustive@latest
|
2021-11-14 20:01:54 +00:00
|
|
|
```
|
|
|
|
|
2022-02-24 16:33:24 +00:00
|
|
|
For docs on the flags, the definition of enum, and the definition of
|
|
|
|
exhaustiveness, see [godocs.io][4].
|
2021-11-14 20:01:54 +00:00
|
|
|
|
2022-02-24 16:33:24 +00:00
|
|
|
For the changelog, see [CHANGELOG][changelog] in the wiki.
|
|
|
|
|
|
|
|
The package provides an `Analyzer` that follows the guidelines in the
|
|
|
|
[`go/analysis`][3] package; this should make it possible to integrate
|
|
|
|
exhaustive with your own analysis driver program.
|
2021-11-14 20:01:54 +00:00
|
|
|
|
|
|
|
## Example
|
|
|
|
|
2022-02-24 16:33:24 +00:00
|
|
|
Given the enum
|
2021-11-14 20:01:54 +00:00
|
|
|
|
2022-02-24 16:33:24 +00:00
|
|
|
```go
|
2021-11-14 20:01:54 +00:00
|
|
|
package token
|
|
|
|
|
|
|
|
type Token int
|
|
|
|
|
|
|
|
const (
|
|
|
|
Add Token = iota
|
|
|
|
Subtract
|
|
|
|
Multiply
|
2022-02-24 16:33:24 +00:00
|
|
|
Quotient
|
|
|
|
Remainder
|
2021-11-14 20:01:54 +00:00
|
|
|
)
|
|
|
|
```
|
2022-02-24 16:33:24 +00:00
|
|
|
|
|
|
|
and the switch statement
|
|
|
|
|
|
|
|
```go
|
2021-11-14 20:01:54 +00:00
|
|
|
package calc
|
|
|
|
|
|
|
|
import "token"
|
|
|
|
|
2022-02-24 16:33:24 +00:00
|
|
|
func f(t token.Token) {
|
2021-11-14 20:01:54 +00:00
|
|
|
switch t {
|
|
|
|
case token.Add:
|
|
|
|
case token.Subtract:
|
|
|
|
case token.Multiply:
|
2022-02-24 16:33:24 +00:00
|
|
|
default:
|
2021-11-14 20:01:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-02-24 16:33:24 +00:00
|
|
|
running exhaustive will print
|
2021-11-14 20:01:54 +00:00
|
|
|
|
|
|
|
```
|
|
|
|
calc.go:6:2: missing cases in switch of type token.Token: Quotient, Remainder
|
|
|
|
```
|
|
|
|
|
2022-02-24 16:33:24 +00:00
|
|
|
## Contributing
|
2021-11-14 20:01:54 +00:00
|
|
|
|
2022-02-24 16:33:24 +00:00
|
|
|
Issues and pull requests are welcome. Before making a substantial
|
|
|
|
change, please discuss it in an issue.
|
2021-11-14 20:01:54 +00:00
|
|
|
|
2022-02-24 16:33:24 +00:00
|
|
|
[1]: https://godocs.io/github.com/nishanths/exhaustive
|
|
|
|
[2]: https://godocs.io/github.com/nishanths/exhaustive?status.svg
|
|
|
|
[3]: https://pkg.go.dev/golang.org/x/tools/go/analysis
|
|
|
|
[4]: https://godocs.io/github.com/nishanths/exhaustive
|
|
|
|
[changelog]: https://github.com/nishanths/exhaustive/wiki/CHANGELOG
|