Skip to content

Commit

Permalink
exhaustive: add new flags/settings and related tests
Browse files Browse the repository at this point in the history
New flags:
  ignore-enum-members
  checking-strategy

Deprecated flags:
  ignore-pattern
  • Loading branch information
nishanths committed Nov 4, 2021
1 parent 1b1e26b commit 0a1f41b
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 4 deletions.
12 changes: 9 additions & 3 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,16 @@ linters-settings:
exhaustive:
# check switch statements in generated files also
check-generated: false
# indicates that switch statements are to be considered exhaustive if a
# 'default' case is present, even if all enum members aren't listed in the
# switch
# presence of "default" case in switch statements satisfies exhaustiveness,
# even if all enum members are not listed
default-signifies-exhaustive: false
# enum members matching the supplied regex do not have to be listed in
# switch statements to satisfy exhaustiveness
ignore-enum-members: ""
# strategy to use when checking exhaustiveness of switch statements; one of:
# "name", "value"; see documentation for details:
# https://pkg.go.dev/github.com/nishanths/exhaustive#section-documentation
checking-strategy: "value"

exhaustivestruct:
# Struct Patterns is list of expressions to match struct packages and names
Expand Down
6 changes: 5 additions & 1 deletion pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ var defaultLintersSettings = LintersSettings{
Exhaustive: ExhaustiveSettings{
CheckGenerated: false,
DefaultSignifiesExhaustive: false,
IgnoreEnumMembers: "",
CheckingStrategy: "value",
},
Gofumpt: GofumptSettings{
LangVersion: "",
Expand Down Expand Up @@ -184,7 +186,9 @@ type ErrorLintSettings struct {
type ExhaustiveSettings struct {
CheckGenerated bool `mapstructure:"check-generated"`
DefaultSignifiesExhaustive bool `mapstructure:"default-signifies-exhaustive"`
IgnorePattern string `mapstructure:"ignore-pattern"`
IgnorePattern string `mapstructure:"ignore-pattern"` // Deprecated: this setting has no effect; see IgnoreEnumMembers instead.
IgnoreEnumMembers string `mapstructure:"ignore-enum-members"`
CheckingStrategy string `mapstructure:"checking-strategy"`
}

type ExhaustiveStructSettings struct {
Expand Down
2 changes: 2 additions & 0 deletions pkg/golinters/exhaustive.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func NewExhaustive(settings *config.ExhaustiveSettings) *goanalysis.Linter {
exhaustive.CheckGeneratedFlag: settings.CheckGenerated,
exhaustive.DefaultSignifiesExhaustiveFlag: settings.DefaultSignifiesExhaustive,
exhaustive.IgnorePatternFlag: settings.IgnorePattern,
exhaustive.IgnoreEnumMembersFlag: settings.IgnoreEnumMembers,
exhaustive.CheckingStrategyFlag: settings.CheckingStrategy,
},
}
}
Expand Down
3 changes: 3 additions & 0 deletions test/testdata/configs/exhaustive_ignore_enum_members.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
linters-settings:
exhaustive:
ignore-enum-members: "East$"
4 changes: 4 additions & 0 deletions test/testdata/exhaustive_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const (
West
)

// Should not report missing cases in the switch statement below even though
// some enum members (East, West) are not listed, because the switch statement
// has a 'default' case and the default-signifies-exhaustive setting is true.

func processDirectionDefault(d Direction) {
switch d {
case North, South:
Expand Down
21 changes: 21 additions & 0 deletions test/testdata/exhaustive_ignore_enum_members.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//args: -Eexhaustive
//config_path: testdata/configs/exhaustive_ignore_enum_members.yml
package testdata

type Direction int

const (
North Direction = iota
East
South
West
)

// Should only report East as missing because the enum member West is ignored
// using the ignore-enum-members setting.

func processDirectionIgnoreEnumMembers(d Direction) {
switch d { // ERROR "missing cases in switch of type Direction: East"
case North, South:
}
}

0 comments on commit 0a1f41b

Please sign in to comment.