From 403564fc6f6200fe67c72d38bec0b28e0029903a Mon Sep 17 00:00:00 2001 From: Nishanth Shanmugham Date: Sun, 24 May 2020 22:32:21 +0530 Subject: [PATCH 01/12] wip more add new files run command fixes more --- .golangci.example.yml | 7 ++++++- .golangci.yml | 3 +++ go.mod | 3 ++- go.sum | 10 ++++++++++ pkg/config/config.go | 8 ++++++++ pkg/golinters/exhaustive.go | 25 +++++++++++++++++++++++++ pkg/golinters/goanalysis/runner.go | 3 ++- pkg/lint/lintersdb/manager.go | 5 +++++ test/testdata/exhaustive.go | 12 ++++++++++++ test/testdata/exhaustive/exhaustive.go | 10 ++++++++++ 10 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 pkg/golinters/exhaustive.go create mode 100644 test/testdata/exhaustive.go create mode 100644 test/testdata/exhaustive/exhaustive.go diff --git a/.golangci.example.yml b/.golangci.example.yml index 8ee42020baa4..1b8785e4e451 100644 --- a/.golangci.example.yml +++ b/.golangci.example.yml @@ -98,6 +98,11 @@ linters-settings: # path to a file containing a list of functions to exclude from checking # see https://github.com/kisielk/errcheck#excluding-functions for details exclude: /path/to/file.txt + exhaustive: + # 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 + default-signifies-exhaustive: false funlen: lines: 60 statements: 40 @@ -174,7 +179,7 @@ linters-settings: modules: # List of blocked modules # - github.com/uudashr/go-module: # Blocked module # recommendations: # Recommended modules that should be used instead (Optional) - # - golang.org/x/mod + # - golang.org/x/mod # reason: "`mod` is the official go.mod parser library." # Reason why the recommended module should be used (Optional) versions: # List of blocked module version constraints # - github.com/mitchellh/go-homedir: # Blocked module with version constraint diff --git a/.golangci.yml b/.golangci.yml index 6ea547c8b493..767ccaa4c846 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -9,6 +9,8 @@ linters-settings: - github.com/sirupsen/logrus: "logging is allowed only by logutils.Log" dupl: threshold: 100 + exhaustive: + default-signifies-exhaustive: false funlen: lines: 100 statements: 50 @@ -104,6 +106,7 @@ linters: # don't enable: # - asciicheck + # - exhaustive # - gochecknoglobals # - gocognit # - godot diff --git a/go.mod b/go.mod index 0030f4672d7b..dd947f146863 100644 --- a/go.mod +++ b/go.mod @@ -33,6 +33,7 @@ require ( github.com/mitchellh/go-homedir v1.1.0 github.com/mitchellh/go-ps v1.0.0 github.com/nakabonne/nestif v0.3.0 + github.com/nishanths/exhaustive v0.0.0-20200525081945-8e46705b6132 github.com/pkg/errors v0.9.1 github.com/ryancurrah/gomodguard v1.1.0 github.com/securego/gosec/v2 v2.3.0 @@ -51,7 +52,7 @@ require ( github.com/ultraware/whitespace v0.0.4 github.com/uudashr/gocognit v1.0.1 github.com/valyala/quicktemplate v1.5.0 - golang.org/x/tools v0.0.0-20200502202811-ed308ab3e770 + golang.org/x/tools v0.0.0-20200522201501-cb1345f3a375 gopkg.in/yaml.v2 v2.3.0 honnef.co/go/tools v0.0.1-2020.1.4 mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed diff --git a/go.sum b/go.sum index 03548d90c83c..1206a5689dcb 100644 --- a/go.sum +++ b/go.sum @@ -267,6 +267,12 @@ github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d h1:AREM5mwr4u1 github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d/go.mod h1:o96djdrsSGy3AWPyBgZMAGfxZNfgntdJG+11KU4QvbU= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= +github.com/nishanths/exhaustive v0.0.0-20200524200018-7c84ae2c817e h1:GEPbURDaBByXbzcU3dbMj9BfA6FwoXb4CvMpOBJWF0E= +github.com/nishanths/exhaustive v0.0.0-20200524200018-7c84ae2c817e/go.mod h1:wBEpHwM2OdmeNpdCvRPUlkEbBuaFmcK4Wv8Q7FuGW3c= +github.com/nishanths/exhaustive v0.0.0-20200525073939-b43704fec28f h1:yF7vRlDP7TKuhs2ZiUf4hVmHqox6+NyzA3nT/AEI32Q= +github.com/nishanths/exhaustive v0.0.0-20200525073939-b43704fec28f/go.mod h1:wBEpHwM2OdmeNpdCvRPUlkEbBuaFmcK4Wv8Q7FuGW3c= +github.com/nishanths/exhaustive v0.0.0-20200525081945-8e46705b6132 h1:NjznefjSrral0MiR4KlB41io/d3OklvhcgQUdfZTqJE= +github.com/nishanths/exhaustive v0.0.0-20200525081945-8e46705b6132/go.mod h1:wBEpHwM2OdmeNpdCvRPUlkEbBuaFmcK4Wv8Q7FuGW3c= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.12.0 h1:Iw5WCbBcaAAd0fpRb1c9r5YCylv4XDoCSigm1zLevwU= @@ -511,6 +517,10 @@ golang.org/x/tools v0.0.0-20200502202811-ed308ab3e770 h1:M9Fif0OxNji8w+HvmhVQ8KJ golang.org/x/tools v0.0.0-20200502202811-ed308ab3e770 h1:M9Fif0OxNji8w+HvmhVQ8KJtiZOsjU9RgslJGhn95XE= golang.org/x/tools v0.0.0-20200502202811-ed308ab3e770 h1:M9Fif0OxNji8w+HvmhVQ8KJtiZOsjU9RgslJGhn95XE= golang.org/x/tools v0.0.0-20200502202811-ed308ab3e770/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20200519015757-0d0afa43d58a h1:gILuVKC+ZPD6g/tj6zBOdnOH1ZHI0zZ86+KLMogc6/s= +golang.org/x/tools v0.0.0-20200519015757-0d0afa43d58a/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20200522201501-cb1345f3a375 h1:SjQ2+AKWgZLc1xej6WSzL+Dfs5Uyd5xcZH1mGC411IA= +golang.org/x/tools v0.0.0-20200522201501-cb1345f3a375/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA= diff --git a/pkg/config/config.go b/pkg/config/config.go index c9175fc63897..542d1c83e12d 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -242,6 +242,7 @@ type LintersSettings struct { Testpackage TestpackageSettings Nestif NestifSettings NoLintLint NoLintLintSettings + Exhaustive ExhaustiveSettings Custom map[string]CustomLinterSettings } @@ -339,6 +340,10 @@ type NestifSettings struct { MinComplexity int `mapstructure:"min-complexity"` } +type ExhaustiveSettings struct { + DefaultSignifiesExhaustive bool `mapstructure:"default-signifies-exhaustive"` +} + var defaultLintersSettings = LintersSettings{ Lll: LllSettings{ LineLength: 120, @@ -389,6 +394,9 @@ var defaultLintersSettings = LintersSettings{ Nestif: NestifSettings{ MinComplexity: 5, }, + Exhaustive: ExhaustiveSettings{ + DefaultSignifiesExhaustive: false, + }, } type CustomLinterSettings struct { diff --git a/pkg/golinters/exhaustive.go b/pkg/golinters/exhaustive.go new file mode 100644 index 000000000000..cae37ecc61d3 --- /dev/null +++ b/pkg/golinters/exhaustive.go @@ -0,0 +1,25 @@ +package golinters + +import ( + "github.com/nishanths/exhaustive" + "golang.org/x/tools/go/analysis" + + "github.com/golangci/golangci-lint/pkg/config" + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" +) + +func NewExhaustive(settings *config.ExhaustiveSettings) *goanalysis.Linter { + a := exhaustive.Analyzer + + var cfg map[string]map[string]interface{} + if settings != nil { + cfg = map[string]map[string]interface{}{ + a.Name: { + exhaustive.DefaultSignifiesExhaustiveFlag: settings.DefaultSignifiesExhaustive, + }, + } + } + + return goanalysis.NewLinter(a.Name, a.Doc, []*analysis.Analyzer{a}, cfg). + WithLoadMode(goanalysis.LoadModeTypesInfo) +} diff --git a/pkg/golinters/goanalysis/runner.go b/pkg/golinters/goanalysis/runner.go index d07294245ef4..db193f37bb20 100644 --- a/pkg/golinters/goanalysis/runner.go +++ b/pkg/golinters/goanalysis/runner.go @@ -935,7 +935,8 @@ func sizeOfReflectValueTreeBytes(rv reflect.Value, visitedPtrs map[uintptr]struc return rv.Len() case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, - reflect.Uintptr, reflect.Bool, reflect.Float32, reflect.Float64, reflect.UnsafePointer: + reflect.Uintptr, reflect.Bool, reflect.Float32, reflect.Float64, + reflect.Complex64, reflect.Complex128, reflect.Func, reflect.UnsafePointer: return int(rv.Type().Size()) case reflect.Invalid: return 0 diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 89ca1755dd9e..1e4106e544a5 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -87,9 +87,11 @@ func enableLinterConfigs(lcs []*linter.Config, isEnabled func(lc *linter.Config) func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { var govetCfg *config.GovetSettings var testpackageCfg *config.TestpackageSettings + var exhaustiveCfg *config.ExhaustiveSettings if m.cfg != nil { govetCfg = &m.cfg.LintersSettings.Govet testpackageCfg = &m.cfg.LintersSettings.Testpackage + exhaustiveCfg = &m.cfg.LintersSettings.Exhaustive } const megacheckName = "megacheck" lcs := []*linter.Config{ @@ -275,6 +277,9 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { linter.NewConfig(golinters.NewExportLoopRef()). WithPresets(linter.PresetBugs). WithURL("https://github.com/kyoh86/exportloopref"), + linter.NewConfig(golinters.NewExhaustive(exhaustiveCfg)). + WithPresets(linter.PresetBugs). + WithURL("https://github.com/nishanths/exhaustive"), // nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives linter.NewConfig(golinters.NewNoLintLint()). WithPresets(linter.PresetStyle). diff --git a/test/testdata/exhaustive.go b/test/testdata/exhaustive.go new file mode 100644 index 000000000000..998395bdb6eb --- /dev/null +++ b/test/testdata/exhaustive.go @@ -0,0 +1,12 @@ +//args: -Eexhaustive +package testdata + +import ( + "github.com/golangci/golangci-lint/test/testdata/exhaustive" +) + +func processDirection(d exhaustive.Direction) { + switch d { // ERROR "missing cases in switch of type exhaustive.Direction: East, West" + case North, South: + } +} diff --git a/test/testdata/exhaustive/exhaustive.go b/test/testdata/exhaustive/exhaustive.go new file mode 100644 index 000000000000..7c8819a2bf44 --- /dev/null +++ b/test/testdata/exhaustive/exhaustive.go @@ -0,0 +1,10 @@ +package exhaustive + +type Direction int + +const ( + North Direction = iota + East + West + South +) From 16dac05e8ee1729c15c20b5a1f03601d8bb18124 Mon Sep 17 00:00:00 2001 From: Nishanth Shanmugham Date: Mon, 25 May 2020 13:59:23 +0530 Subject: [PATCH 02/12] go.mod --- go.mod | 2 +- go.sum | 6 ------ 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/go.mod b/go.mod index dd947f146863..867d6132ed16 100644 --- a/go.mod +++ b/go.mod @@ -52,7 +52,7 @@ require ( github.com/ultraware/whitespace v0.0.4 github.com/uudashr/gocognit v1.0.1 github.com/valyala/quicktemplate v1.5.0 - golang.org/x/tools v0.0.0-20200522201501-cb1345f3a375 + golang.org/x/tools v0.0.0-20200519015757-0d0afa43d58a gopkg.in/yaml.v2 v2.3.0 honnef.co/go/tools v0.0.1-2020.1.4 mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed diff --git a/go.sum b/go.sum index 1206a5689dcb..8f8c61c376ec 100644 --- a/go.sum +++ b/go.sum @@ -267,10 +267,6 @@ github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d h1:AREM5mwr4u1 github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d/go.mod h1:o96djdrsSGy3AWPyBgZMAGfxZNfgntdJG+11KU4QvbU= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= -github.com/nishanths/exhaustive v0.0.0-20200524200018-7c84ae2c817e h1:GEPbURDaBByXbzcU3dbMj9BfA6FwoXb4CvMpOBJWF0E= -github.com/nishanths/exhaustive v0.0.0-20200524200018-7c84ae2c817e/go.mod h1:wBEpHwM2OdmeNpdCvRPUlkEbBuaFmcK4Wv8Q7FuGW3c= -github.com/nishanths/exhaustive v0.0.0-20200525073939-b43704fec28f h1:yF7vRlDP7TKuhs2ZiUf4hVmHqox6+NyzA3nT/AEI32Q= -github.com/nishanths/exhaustive v0.0.0-20200525073939-b43704fec28f/go.mod h1:wBEpHwM2OdmeNpdCvRPUlkEbBuaFmcK4Wv8Q7FuGW3c= github.com/nishanths/exhaustive v0.0.0-20200525081945-8e46705b6132 h1:NjznefjSrral0MiR4KlB41io/d3OklvhcgQUdfZTqJE= github.com/nishanths/exhaustive v0.0.0-20200525081945-8e46705b6132/go.mod h1:wBEpHwM2OdmeNpdCvRPUlkEbBuaFmcK4Wv8Q7FuGW3c= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= @@ -519,8 +515,6 @@ golang.org/x/tools v0.0.0-20200502202811-ed308ab3e770 h1:M9Fif0OxNji8w+HvmhVQ8KJ golang.org/x/tools v0.0.0-20200502202811-ed308ab3e770/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20200519015757-0d0afa43d58a h1:gILuVKC+ZPD6g/tj6zBOdnOH1ZHI0zZ86+KLMogc6/s= golang.org/x/tools v0.0.0-20200519015757-0d0afa43d58a/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= -golang.org/x/tools v0.0.0-20200522201501-cb1345f3a375 h1:SjQ2+AKWgZLc1xej6WSzL+Dfs5Uyd5xcZH1mGC411IA= -golang.org/x/tools v0.0.0-20200522201501-cb1345f3a375/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA= From 870488b78df047bb2709e45b273e26bc7f67830b Mon Sep 17 00:00:00 2001 From: Nishanth Shanmugham Date: Mon, 25 May 2020 14:11:13 +0530 Subject: [PATCH 03/12] order --- test/testdata/exhaustive/exhaustive.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testdata/exhaustive/exhaustive.go b/test/testdata/exhaustive/exhaustive.go index 7c8819a2bf44..248cacedbdb4 100644 --- a/test/testdata/exhaustive/exhaustive.go +++ b/test/testdata/exhaustive/exhaustive.go @@ -5,6 +5,6 @@ type Direction int const ( North Direction = iota East - West South + West ) From cfb270e9d576e5435e3be1e73afca9be1399854a Mon Sep 17 00:00:00 2001 From: Nishanth Shanmugham Date: Mon, 25 May 2020 14:19:19 +0530 Subject: [PATCH 04/12] same package --- test/testdata/exhaustive.go | 13 +++++++++---- test/testdata/exhaustive/exhaustive.go | 10 ---------- 2 files changed, 9 insertions(+), 14 deletions(-) delete mode 100644 test/testdata/exhaustive/exhaustive.go diff --git a/test/testdata/exhaustive.go b/test/testdata/exhaustive.go index 998395bdb6eb..171c6e817fc8 100644 --- a/test/testdata/exhaustive.go +++ b/test/testdata/exhaustive.go @@ -1,12 +1,17 @@ //args: -Eexhaustive package testdata -import ( - "github.com/golangci/golangci-lint/test/testdata/exhaustive" +type Direction int + +const ( + North Direction = iota + East + South + West ) -func processDirection(d exhaustive.Direction) { - switch d { // ERROR "missing cases in switch of type exhaustive.Direction: East, West" +func processDirection(d Direction) { + switch d { // ERROR "missing cases in switch of type Direction: East, West" case North, South: } } diff --git a/test/testdata/exhaustive/exhaustive.go b/test/testdata/exhaustive/exhaustive.go deleted file mode 100644 index 248cacedbdb4..000000000000 --- a/test/testdata/exhaustive/exhaustive.go +++ /dev/null @@ -1,10 +0,0 @@ -package exhaustive - -type Direction int - -const ( - North Direction = iota - East - South - West -) From 1de022410268affd52a04f88718d1ee7e92eb84e Mon Sep 17 00:00:00 2001 From: Nishanth Shanmugham Date: Mon, 25 May 2020 22:07:02 +0530 Subject: [PATCH 05/12] review comment --- pkg/lint/lintersdb/manager.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 1e4106e544a5..893f5aafc7e3 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -279,6 +279,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithURL("https://github.com/kyoh86/exportloopref"), linter.NewConfig(golinters.NewExhaustive(exhaustiveCfg)). WithPresets(linter.PresetBugs). + WithLoadForGoAnalysis(). WithURL("https://github.com/nishanths/exhaustive"), // nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives linter.NewConfig(golinters.NewNoLintLint()). From 082eb00ece6dc3e24166b2c87d169a51687e4ac0 Mon Sep 17 00:00:00 2001 From: Nishanth Shanmugham Date: Mon, 25 May 2020 23:09:28 +0530 Subject: [PATCH 06/12] enable linter in .golangci.yml --- .golangci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.golangci.yml b/.golangci.yml index 767ccaa4c846..6d2c3f84b921 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -73,6 +73,7 @@ linters: - dogsled - dupl - errcheck + - exhaustive - funlen - gochecknoinits - goconst @@ -106,7 +107,6 @@ linters: # don't enable: # - asciicheck - # - exhaustive # - gochecknoglobals # - gocognit # - godot From cf8f8f025e8763c071b542ff4fde6a525f1ce9b5 Mon Sep 17 00:00:00 2001 From: Nishanth Shanmugham Date: Tue, 26 May 2020 01:57:36 +0530 Subject: [PATCH 07/12] add testcase for default-signifies-exhaustive: true --- test/testdata/configs/exhaustive.yml | 3 +++ test/testdata/exhaustive_default.go | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 test/testdata/configs/exhaustive.yml create mode 100644 test/testdata/exhaustive_default.go diff --git a/test/testdata/configs/exhaustive.yml b/test/testdata/configs/exhaustive.yml new file mode 100644 index 000000000000..03f2bf902863 --- /dev/null +++ b/test/testdata/configs/exhaustive.yml @@ -0,0 +1,3 @@ +linters-settings: + exhaustive: + default-signifies-exhaustive: true diff --git a/test/testdata/exhaustive_default.go b/test/testdata/exhaustive_default.go new file mode 100644 index 000000000000..c87c7becf9b9 --- /dev/null +++ b/test/testdata/exhaustive_default.go @@ -0,0 +1,25 @@ +//args: -Eexhaustive +//config_path: testdata/configs/exhaustive.yml +package testdata + +type Direction int + +const ( + North Direction = iota + East + South + West +) + +func processDirection(d Direction) { + switch d { // ERROR "missing cases in switch of type Direction: East, West" + case North, South: + } +} + +func processDirectionDefault(d Direction) { + switch d { + case North, South: + default: + } +} From a29296772a989017febe3c9d6138e16d4a214179 Mon Sep 17 00:00:00 2001 From: Nishanth Shanmugham Date: Tue, 26 May 2020 02:06:45 +0530 Subject: [PATCH 08/12] adjust runGoErrchk instead --- test/linters_test.go | 11 ++++++++--- test/testdata/exhaustive_default.go | 6 ------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/test/linters_test.go b/test/linters_test.go index b53fc14c82bc..77633cc2446a 100644 --- a/test/linters_test.go +++ b/test/linters_test.go @@ -17,9 +17,14 @@ import ( func runGoErrchk(c *exec.Cmd, files []string, t *testing.T) { output, err := c.CombinedOutput() - assert.Error(t, err) - _, ok := err.(*exec.ExitError) - assert.True(t, ok, err) + // The returned error will be nil if the test does not write to stderr (some test + // cases produce no output). Perform these assertions only if the error is + // present. + if err != nil { + assert.Error(t, err) + _, ok := err.(*exec.ExitError) + assert.True(t, ok, err) + } // TODO: uncomment after deprecating go1.11 // assert.Equal(t, exitcodes.IssuesFound, exitErr.ExitCode()) diff --git a/test/testdata/exhaustive_default.go b/test/testdata/exhaustive_default.go index c87c7becf9b9..36c1dee6ed03 100644 --- a/test/testdata/exhaustive_default.go +++ b/test/testdata/exhaustive_default.go @@ -11,12 +11,6 @@ const ( West ) -func processDirection(d Direction) { - switch d { // ERROR "missing cases in switch of type Direction: East, West" - case North, South: - } -} - func processDirectionDefault(d Direction) { switch d { case North, South: From 46838643dcdd3d3327f0977ce24217a90881d8e7 Mon Sep 17 00:00:00 2001 From: Nishanth Shanmugham Date: Tue, 26 May 2020 02:09:58 +0530 Subject: [PATCH 09/12] disable the linter --- .golangci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.golangci.yml b/.golangci.yml index 6d2c3f84b921..669b96da5beb 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -73,7 +73,6 @@ linters: - dogsled - dupl - errcheck - - exhaustive - funlen - gochecknoinits - goconst @@ -107,6 +106,7 @@ linters: # don't enable: # - asciicheck + # - exhaustive (TODO: enable after next release) # - gochecknoglobals # - gocognit # - godot From f83ef763f72ae660de83dcd6fc2eb7ead5bc1f5d Mon Sep 17 00:00:00 2001 From: Nishanth Shanmugham Date: Tue, 26 May 2020 02:12:38 +0530 Subject: [PATCH 10/12] cleanup --- test/linters_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/test/linters_test.go b/test/linters_test.go index 77633cc2446a..530f5fcf0208 100644 --- a/test/linters_test.go +++ b/test/linters_test.go @@ -21,7 +21,6 @@ func runGoErrchk(c *exec.Cmd, files []string, t *testing.T) { // cases produce no output). Perform these assertions only if the error is // present. if err != nil { - assert.Error(t, err) _, ok := err.(*exec.ExitError) assert.True(t, ok, err) } From 7ec55f0555f10c6e18fbee9cb3006889a85ded3e Mon Sep 17 00:00:00 2001 From: Nishanth Shanmugham Date: Tue, 26 May 2020 02:18:25 +0530 Subject: [PATCH 11/12] more cleanup --- .golangci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.golangci.yml b/.golangci.yml index 669b96da5beb..0243834ece9a 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -106,7 +106,7 @@ linters: # don't enable: # - asciicheck - # - exhaustive (TODO: enable after next release) + # - exhaustive (TODO: enable after next release; current release is v1.27) # - gochecknoglobals # - gocognit # - godot From f14ddaee2b2f3ac48de9e87bab00f5fa332150cb Mon Sep 17 00:00:00 2001 From: Nishanth Shanmugham Date: Tue, 26 May 2020 15:16:32 +0530 Subject: [PATCH 12/12] cleanup --- .golangci.yml | 2 +- test/linters_test.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 0243834ece9a..a592fecf0c38 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -106,7 +106,7 @@ linters: # don't enable: # - asciicheck - # - exhaustive (TODO: enable after next release; current release is v1.27) + # - exhaustive (TODO: enable after next release; current release at time of writing is v1.27) # - gochecknoglobals # - gocognit # - godot diff --git a/test/linters_test.go b/test/linters_test.go index 530f5fcf0208..ac57f4942ac0 100644 --- a/test/linters_test.go +++ b/test/linters_test.go @@ -17,9 +17,9 @@ import ( func runGoErrchk(c *exec.Cmd, files []string, t *testing.T) { output, err := c.CombinedOutput() - // The returned error will be nil if the test does not write to stderr (some test - // cases produce no output). Perform these assertions only if the error is - // present. + // The returned error will be nil if the test file does not have any issues + // and thus the linter exits with exit code 0. So perform the additional + // assertions only if the error is non-nil. if err != nil { _, ok := err.(*exec.ExitError) assert.True(t, ok, err)