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

exhaustive: upgrade to latest tag; add new flags and deprecate old ones #2344

Merged
merged 3 commits into from
Nov 4, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
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
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_checking_strategy_name.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
linters-settings:
exhaustive:
checking-strategy: "name"
3 changes: 3 additions & 0 deletions test/testdata/configs/exhaustive_checking_strategy_value.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
linters-settings:
exhaustive:
checking-strategy: "value"
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: "West$"
18 changes: 18 additions & 0 deletions test/testdata/exhaustive_checking_strategy_name.go
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
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:
}
}