Skip to content

Commit 054fc3f

Browse files
authored
exhaustive: upgrade to v0.3.6; add new flags and deprecate old ones (#2344)
1 parent 65c47cb commit 054fc3f

12 files changed

+93
-7
lines changed

.golangci.example.yml

+9-3
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,16 @@ linters-settings:
136136
exhaustive:
137137
# check switch statements in generated files also
138138
check-generated: false
139-
# indicates that switch statements are to be considered exhaustive if a
140-
# 'default' case is present, even if all enum members aren't listed in the
141-
# switch
139+
# presence of "default" case in switch statements satisfies exhaustiveness,
140+
# even if all enum members are not listed
142141
default-signifies-exhaustive: false
142+
# enum members matching the supplied regex do not have to be listed in
143+
# switch statements to satisfy exhaustiveness
144+
ignore-enum-members: ""
145+
# strategy to use when checking exhaustiveness of switch statements; one of:
146+
# "name", "value"; see documentation for details:
147+
# https://pkg.go.dev/github.com/nishanths/exhaustive#section-documentation
148+
checking-strategy: "value"
143149

144150
exhaustivestruct:
145151
# Struct Patterns is list of expressions to match struct packages and names

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ require (
5959
github.com/mitchellh/go-ps v1.0.0
6060
github.com/moricho/tparallel v0.2.1
6161
github.com/nakabonne/nestif v0.3.1
62-
github.com/nishanths/exhaustive v0.2.3
62+
github.com/nishanths/exhaustive v0.3.6
6363
github.com/nishanths/predeclared v0.2.1
6464
github.com/pkg/errors v0.9.1
6565
github.com/polyfloyd/go-errorlint v0.0.0-20210722154253-910bb7978349

go.sum

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/linters_settings.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ var defaultLintersSettings = LintersSettings{
5757
Exhaustive: ExhaustiveSettings{
5858
CheckGenerated: false,
5959
DefaultSignifiesExhaustive: false,
60+
IgnoreEnumMembers: "",
61+
CheckingStrategy: "value",
6062
},
6163
Gofumpt: GofumptSettings{
6264
LangVersion: "",
@@ -184,7 +186,9 @@ type ErrorLintSettings struct {
184186
type ExhaustiveSettings struct {
185187
CheckGenerated bool `mapstructure:"check-generated"`
186188
DefaultSignifiesExhaustive bool `mapstructure:"default-signifies-exhaustive"`
187-
IgnorePattern string `mapstructure:"ignore-pattern"`
189+
IgnorePattern string `mapstructure:"ignore-pattern"` // Deprecated: this setting has no effect; see IgnoreEnumMembers instead.
190+
IgnoreEnumMembers string `mapstructure:"ignore-enum-members"`
191+
CheckingStrategy string `mapstructure:"checking-strategy"`
188192
}
189193

190194
type ExhaustiveStructSettings struct {

pkg/golinters/exhaustive.go

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ func NewExhaustive(settings *config.ExhaustiveSettings) *goanalysis.Linter {
1818
exhaustive.CheckGeneratedFlag: settings.CheckGenerated,
1919
exhaustive.DefaultSignifiesExhaustiveFlag: settings.DefaultSignifiesExhaustive,
2020
exhaustive.IgnorePatternFlag: settings.IgnorePattern,
21+
exhaustive.IgnoreEnumMembersFlag: settings.IgnoreEnumMembers,
22+
exhaustive.CheckingStrategyFlag: settings.CheckingStrategy,
2123
},
2224
}
2325
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
linters-settings:
2+
exhaustive:
3+
checking-strategy: "name"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
linters-settings:
2+
exhaustive:
3+
checking-strategy: "value"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
linters-settings:
2+
exhaustive:
3+
ignore-enum-members: "West$"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//args: -Eexhaustive
2+
//config_path: testdata/configs/exhaustive_checking_strategy_name.yml
3+
package testdata
4+
5+
type AccessControl string
6+
7+
const (
8+
AccessPublic AccessControl = "public"
9+
AccessPrivate AccessControl = "private"
10+
AccessDefault AccessControl = AccessPublic
11+
)
12+
13+
func example(v AccessControl) {
14+
switch v { // ERROR "missing cases in switch of type AccessControl: AccessDefault"
15+
case AccessPublic:
16+
case AccessPrivate:
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//args: -Eexhaustive
2+
//config_path: testdata/configs/exhaustive_checking_strategy_value.yml
3+
package testdata
4+
5+
type AccessControl string
6+
7+
const (
8+
AccessPublic AccessControl = "public"
9+
AccessPrivate AccessControl = "private"
10+
AccessDefault AccessControl = AccessPublic
11+
)
12+
13+
// Expect no diagnostics for this switch statement, even though AccessDefault is
14+
// not listed, because AccessPublic (which is listed) has the same value as
15+
// AccessDefault.
16+
17+
func example(v AccessControl) {
18+
switch v {
19+
case AccessPublic:
20+
case AccessPrivate:
21+
}
22+
}

test/testdata/exhaustive_default.go

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ const (
1111
West
1212
)
1313

14+
// Should not report missing cases in the switch statement below even though
15+
// some enum members (East, West) are not listed, because the switch statement
16+
// has a 'default' case and the default-signifies-exhaustive setting is true.
17+
1418
func processDirectionDefault(d Direction) {
1519
switch d {
1620
case North, South:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//args: -Eexhaustive
2+
//config_path: testdata/configs/exhaustive_ignore_enum_members.yml
3+
package testdata
4+
5+
type Direction int
6+
7+
const (
8+
North Direction = iota
9+
East
10+
South
11+
West
12+
)
13+
14+
// Should only report East as missing because the enum member West is ignored
15+
// using the ignore-enum-members setting.
16+
17+
func processDirectionIgnoreEnumMembers(d Direction) {
18+
switch d { // ERROR "missing cases in switch of type Direction: East"
19+
case North, South:
20+
}
21+
}

0 commit comments

Comments
 (0)