Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use '-maps' flag to enable checking maps #50

Merged
merged 1 commit into from Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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