Skip to content

Commit

Permalink
-maps flag to enable checking maps (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
nishanths committed Aug 15, 2022
1 parent fad1089 commit dba24e6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
16 changes: 13 additions & 3 deletions exhaustive.go
Expand Up @@ -56,6 +56,7 @@ Map literals
All above relates to map literals as well, if key is an enum type.
But empty map is ignored because it's an alternative for make(map...).
The -maps flag must be enabled.
Type aliases
Expand Down Expand Up @@ -119,13 +120,16 @@ All of these flags are optional.
flag type default value
-maps bool false
-explicit-exhaustive-switch bool false
-explicit-exhaustive-map bool false
-check-generated bool false
-default-signifies-exhaustive bool false
-ignore-enum-members string (none)
-package-scope-only bool false
If the -maps flag is enabled, the analyzer additionally checks map literals
having enum key types for exhaustiveness.
If the -explicit-exhaustive-switch flag is enabled, the analyzer only runs on
switch statements explicitly marked with the comment text
Expand Down Expand Up @@ -226,6 +230,7 @@ func (v *regexpFlag) Set(expr string) error {
func (v *regexpFlag) value() *regexp.Regexp { return v.r }

func init() {
Analyzer.Flags.BoolVar(&fCheckMaps, CheckMapsFlag, false, "additionally check map literals of enum key type for exhaustiveness")
Analyzer.Flags.BoolVar(&fExplicitExhaustiveSwitch, ExplicitExhaustiveSwitchFlag, false, "only run exhaustive check on switches with \"//exhaustive:enforce\" comment")
Analyzer.Flags.BoolVar(&fExplicitExhaustiveMap, ExplicitExhaustiveMapFlag, false, "only run exhaustive check on map literals with \"//exhaustive:enforce\" comment")
Analyzer.Flags.BoolVar(&fCheckGenerated, CheckGeneratedFlag, false, "check switch statements in generated files")
Expand All @@ -241,6 +246,7 @@ func init() {
// Flag names used by the analyzer. They are exported for use by analyzer
// driver programs.
const (
CheckMapsFlag = "maps"
ExplicitExhaustiveSwitchFlag = "explicit-exhaustive-switch"
ExplicitExhaustiveMapFlag = "explicit-exhaustive-map"
CheckGeneratedFlag = "check-generated"
Expand All @@ -253,6 +259,7 @@ const (
)

var (
fCheckMaps bool
fExplicitExhaustiveSwitch bool
fExplicitExhaustiveMap bool
fCheckGenerated bool
Expand All @@ -264,6 +271,7 @@ var (
// resetFlags resets the flag variables to their default values.
// Useful in tests.
func resetFlags() {
fCheckMaps = false
fExplicitExhaustiveSwitch = false
fExplicitExhaustiveMap = false
fCheckGenerated = false
Expand Down Expand Up @@ -313,12 +321,14 @@ func run(pass *analysis.Pass) (interface{}, error) {
comments,
)

filter := []ast.Node{
types := []ast.Node{
&ast.SwitchStmt{},
&ast.CompositeLit{},
}
if fCheckMaps {
types = append(types, &ast.CompositeLit{})
}

inspect.WithStack(filter, func(n ast.Node, push bool, stack []ast.Node) bool {
inspect.WithStack(types, func(n ast.Node, push bool, stack []ast.Node) bool {
var proceed bool
switch n.(type) {
case *ast.SwitchStmt:
Expand Down
1 change: 1 addition & 0 deletions exhaustive_test.go
Expand Up @@ -76,6 +76,7 @@ func TestExhaustive(t *testing.T) {
t.Helper()
t.Run(pattern, func(t *testing.T) {
resetFlags()
fCheckMaps = true // default to true for test
for _, f := range setup {
f()
}
Expand Down
3 changes: 2 additions & 1 deletion map.go
Expand Up @@ -4,9 +4,10 @@ import (
"fmt"
"go/ast"
"go/types"
"golang.org/x/tools/go/analysis"
"regexp"
"strings"

"golang.org/x/tools/go/analysis"
)

// mapConfig is configuration for mapChecker.
Expand Down

0 comments on commit dba24e6

Please sign in to comment.