Skip to content

Commit

Permalink
exhaustive: upgrade to v0.3.6; add new flags and deprecate old ones (g…
Browse files Browse the repository at this point in the history
  • Loading branch information
nishanths authored and SeigeC committed Apr 4, 2023
1 parent 6d0194b commit 59f60f5
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 7 deletions.
12 changes: 9 additions & 3 deletions .golangci.example.yml
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
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -59,7 +59,7 @@ require (
github.com/mitchellh/go-ps v1.0.0
github.com/moricho/tparallel v0.2.1
github.com/nakabonne/nestif v0.3.1
github.com/nishanths/exhaustive v0.2.3
github.com/nishanths/exhaustive v0.3.6
github.com/nishanths/predeclared v0.2.1
github.com/pkg/errors v0.9.1
github.com/polyfloyd/go-errorlint v0.0.0-20210722154253-910bb7978349
Expand Down
4 changes: 2 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion pkg/config/linters_settings.go
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
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_checking_strategy_name.yml
@@ -0,0 +1,3 @@
linters-settings:
exhaustive:
checking-strategy: "name"
3 changes: 3 additions & 0 deletions test/testdata/configs/exhaustive_checking_strategy_value.yml
@@ -0,0 +1,3 @@
linters-settings:
exhaustive:
checking-strategy: "value"
3 changes: 3 additions & 0 deletions test/testdata/configs/exhaustive_ignore_enum_members.yml
@@ -0,0 +1,3 @@
linters-settings:
exhaustive:
ignore-enum-members: "West$"
18 changes: 18 additions & 0 deletions test/testdata/exhaustive_checking_strategy_name.go
@@ -0,0 +1,18 @@
//args: -Eexhaustive
//config_path: testdata/configs/exhaustive_checking_strategy_name.yml
package testdata

type AccessControl string

const (
AccessPublic AccessControl = "public"
AccessPrivate AccessControl = "private"
AccessDefault AccessControl = AccessPublic
)

func example(v AccessControl) {
switch v { // ERROR "missing cases in switch of type AccessControl: AccessDefault"
case AccessPublic:
case AccessPrivate:
}
}
22 changes: 22 additions & 0 deletions test/testdata/exhaustive_checking_strategy_value.go
@@ -0,0 +1,22 @@
//args: -Eexhaustive
//config_path: testdata/configs/exhaustive_checking_strategy_value.yml
package testdata

type AccessControl string

const (
AccessPublic AccessControl = "public"
AccessPrivate AccessControl = "private"
AccessDefault AccessControl = AccessPublic
)

// Expect no diagnostics for this switch statement, even though AccessDefault is
// not listed, because AccessPublic (which is listed) has the same value as
// AccessDefault.

func example(v AccessControl) {
switch v {
case AccessPublic:
case AccessPrivate:
}
}
4 changes: 4 additions & 0 deletions test/testdata/exhaustive_default.go
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
@@ -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 59f60f5

Please sign in to comment.