• Stars
    star
    296
  • Rank 140,464 (Top 3 %)
  • Language
    Go
  • License
    BSD 2-Clause "Sim...
  • Created over 4 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Check exhaustiveness of switch statements of enum-like constants in Go source code.

exhaustive

Godoc

exhaustive checks exhaustiveness of enum switch statements in Go source code.

For the definition of enum and the definition of exhaustiveness used by this program, see godoc. For the changelog, see CHANGELOG in the GitHub wiki. The program can be configured to additionally check exhaustiveness of keys in map literals whose key type is an enum.

Usage

Command:

go install github.com/nishanths/exhaustive/cmd/exhaustive@latest

exhaustive [flags] [packages]

For available flags, refer to the Flags section in godoc or run exhaustive -h.

Package:

go get github.com/nishanths/exhaustive

import "github.com/nishanths/exhaustive"

The exhaustive.Analyzer variable follows guidelines in the golang.org/x/tools/go/analysis package. This should make it possible to integrate exhaustive with your own analysis driver program.

Example

Given an enum:

package token // import "example.org/token"

type Token int

const (
	Add Token = iota
	Subtract
	Multiply
	Quotient
	Remainder
)

and code that switches on the enum:

package calc

import "example.org/token"

func x(t token.Token) {
	switch t {
	case token.Add:
	case token.Subtract:
	case token.Remainder:
	default:
	}
}

running exhaustive with default flags will produce:

calc.go:6:2: missing cases in switch of type token.Token: token.Multiply, token.Quotient

Specify flag -check=switch,map to additionally check exhaustiveness of keys in map literals. For example:

var m = map[token.Token]rune{
	token.Add:      '+',
	token.Subtract: '-',
	token.Multiply: '*',
	token.Quotient: '/',
}
calc.go:14:9: missing keys in map of key type token.Token: token.Remainder

Contributing

Issues and changes are welcome. Please discuss substantial changes in an issue first.

More Repositories

1

zoom.js

Fork of fat/zoom.js with improvements
TypeScript
1,348
star
2

license

Command line license text generator.
Go
664
star
3

cocoa-hugo-theme

Responsive Hugo blog theme (note: not actively worked on)
CSS
326
star
4

go-xkcd

xkcd.com API client in Go
Go
51
star
5

lyft

Create and manage Lyft rides from the command line
Go
36
star
6

predeclared

Find declarations in Go source code that shadow predeclared identifiers
Go
28
star
7

loupe-js

Image magnifier written in TypeScript; works with touch events and supports React
TypeScript
20
star
8

slack-texts

SMS notifications for Slack groups
JavaScript
19
star
9

dedupimport

Deduplicate named and unnamed imports of the same import path in Go files
Go
18
star
10

youtube-pause-chrome

Properly pause YouTube with spacebar
JavaScript
16
star
11

YWeatherAPI

Yahoo Weather API wrapper for iOS and Mac
Objective-C
16
star
12

rust-haikunator

Rust
16
star
13

scrobble

Apple Music scrobbling with beautiful album artwork.
Go
10
star
14

what-s-playing

Firefox extension: Desktop notifications for music websites
JavaScript
7
star
15

block-urls

Firefox WebExtension to block exact URLs
JavaScript
4
star
16

QuickCal

Chrome extension: Single-click event adding to Google Calendar
JavaScript
4
star
17

nishanths.github.io

Website + writing with Jekyll (see 'source' branch for old code).
CSS
4
star
18

lyft-go

Go client for Lyft's v1 HTTP API
Go
3
star
19

metaimport

Generate HTML pages with <meta> tags for custom Go import paths
Go
3
star
20

print-instacart

Make a decent PDF of an Instacart order
TypeScript
2
star
21

redditgold

Gold features such as new comment highlighting
JavaScript
2
star
22

godoc-goos-goarch

Easily switch GOOS and GOARCH params on godoc.org
HTML
2
star
23

fullstory

Go client for the FullStory API
Go
1
star
24

albumday

β€œAlbum Birthdays!” app source code
Go
1
star
25

unusedargs

See instead: https://github.com/mvdan/unparam
Go
1
star
26

fmap

Generate a map[string][]byte of directory contents
Go
1
star
27

iterators.js

Iterators for node and the browser
JavaScript
1
star
28

spitfire-live

A real-time, personal lyrics assistant (winning project @HackTX 2015)
HTML
1
star
29

auto-browser

Automate the browser based on natural actions – Top 10 @HackTX 2014
JavaScript
1
star
30

impl

Find types that implement an interface
Go
1
star
31

chp

Common Go channel patterns implemented with type parameters.
Go
1
star