From f647e90e90aaedb8afa50e53ff02629075136146 Mon Sep 17 00:00:00 2001 From: Oleg Butuzov Date: Sun, 5 Sep 2021 14:25:49 +0300 Subject: [PATCH 01/12] new-linter: ireturn (checks for function return type) --- .golangci.example.yml | 21 +++++++++++++++++++ go.mod | 3 +++ go.sum | 4 ++++ pkg/config/linters_settings.go | 6 ++++++ pkg/golinters/ireturn.go | 35 ++++++++++++++++++++++++++++++++ pkg/lint/lintersdb/manager.go | 8 ++++++++ test/testdata/ireturn_allow.go | 13 ++++++++++++ test/testdata/ireturn_default.go | 12 +++++++++++ 8 files changed, 102 insertions(+) create mode 100644 pkg/golinters/ireturn.go create mode 100644 test/testdata/ireturn_allow.go create mode 100644 test/testdata/ireturn_default.go diff --git a/.golangci.example.yml b/.golangci.example.yml index ac0577c6ad7a..34033fa565ac 100644 --- a/.golangci.example.yml +++ b/.golangci.example.yml @@ -447,6 +447,27 @@ linters-settings: - pkg: knative.dev/serving/pkg/apis/(\w+)/(v[\w\d]+) alias: $1$2 + ireturn: + # ireturn allows to use either `allow` or `reject` settings at the same time. + # both settings are a lists of the keywords (`empty` for interface{}`, + # `error` for errors, `stdlib` for standard library, `anon` for anonymouse + # interfaces ) and reglar expresssions matched to interfaces or packages. + + allow: + # default settings are allow to use errors, empty interfaces, anonymouse + # interfaces, and interfaces provided by standard library. + - anon + - error + - empty + - stdlib + # You can specify idiomatic endings for interface + - (or|er)$ + + + # or you can specify reject patterns + reject: + - github.com\/user\/package\/v4\.Type + lll: # max line length, lines longer will be reported. Default is 120. # '\t' is counted as 1 character by default, and can be changed with the tab-width option diff --git a/go.mod b/go.mod index e039bd1670a9..46aa3ce23452 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( github.com/ashanbrown/makezero v0.0.0-20210520155254-b6261585ddde github.com/bkielbasa/cyclop v1.2.0 github.com/bombsimon/wsl/v3 v3.3.0 + github.com/butuzov/ireturn v0.0.0-20210903145654-645c1cbdbffd // indirect github.com/charithe/durationcheck v0.0.8 github.com/daixiang0/gci v0.2.9 github.com/denis-tingajkin/go-header v0.4.2 @@ -92,3 +93,5 @@ require ( mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b // indirect mvdan.cc/unparam v0.0.0-20210104141923-aac4ce9116a7 ) + +replace github.com/butuzov/ireturn => ../ireturn diff --git a/go.sum b/go.sum index 392f5cf8d858..c9433203fbb7 100644 --- a/go.sum +++ b/go.sum @@ -95,6 +95,8 @@ github.com/bkielbasa/cyclop v1.2.0 h1:7Jmnh0yL2DjKfw28p86YTd/B4lRGcNuu12sKE35sM7 github.com/bkielbasa/cyclop v1.2.0/go.mod h1:qOI0yy6A7dYC4Zgsa72Ppm9kONl0RoIlPbzot9mhmeI= github.com/bombsimon/wsl/v3 v3.3.0 h1:Mka/+kRLoQJq7g2rggtgQsjuI/K5Efd87WX96EWFxjM= github.com/bombsimon/wsl/v3 v3.3.0/go.mod h1:st10JtZYLE4D5sC7b8xV4zTKZwAQjCH/Hy2Pm1FNZIc= +github.com/butuzov/ireturn v0.0.0-20210903145654-645c1cbdbffd h1:FCBvjaeDPGFMZpUeUrEikCrhL/Bp9UOUfhRTSBrkcgA= +github.com/butuzov/ireturn v0.0.0-20210903145654-645c1cbdbffd/go.mod h1:pxsIfiT9YhERToguQDHMdthS36WZBCltDcoh5dpbd2M= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= @@ -280,6 +282,8 @@ github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= +github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index fd5f413181ca..94fd9114643d 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -110,6 +110,7 @@ type LintersSettings struct { Gosimple StaticCheckSettings Govet GovetSettings Ifshort IfshortSettings + Ireturn IreturnSettings ImportAs ImportAsSettings Lll LllSettings Makezero MakezeroSettings @@ -186,6 +187,11 @@ type ExhaustiveStructSettings struct { StructPatterns []string `mapstructure:"struct-patterns"` } +type IreturnSettings struct { + Allow []string `mapstructure:"allow"` + Reject []string `mapstructure:"return"` +} + type ForbidigoSettings struct { Forbid []string `mapstructure:"forbid"` ExcludeGodocExamples bool `mapstructure:"exclude-godoc-examples"` diff --git a/pkg/golinters/ireturn.go b/pkg/golinters/ireturn.go new file mode 100644 index 000000000000..c26b65059aef --- /dev/null +++ b/pkg/golinters/ireturn.go @@ -0,0 +1,35 @@ +package golinters + +import ( + "strings" + + "github.com/golangci/golangci-lint/pkg/config" + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" + + "github.com/butuzov/ireturn/analyzer" + "golang.org/x/tools/go/analysis" +) + +func NewIreturn(settings *config.IreturnSettings) *goanalysis.Linter { + a := analyzer.NewAnalyzer() + + return goanalysis.NewLinter( + a.Name, + a.Doc, + []*analysis.Analyzer{a}, + ireturnSettings(a.Name, settings), + ).WithLoadMode(goanalysis.LoadModeTypesInfo) +} + +func ireturnSettings(name string, s *config.IreturnSettings) map[string]map[string]interface{} { + if s == nil { + return nil + } + + return map[string]map[string]interface{}{ + name: { + "allow": strings.Join(s.Allow, ","), + "reject": strings.Join(s.Reject, ","), + }, + } +} diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index a69a6ec38261..819312d53c6f 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -110,6 +110,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { var reviveCfg *config.ReviveSettings var cyclopCfg *config.Cyclop var importAsCfg *config.ImportAsSettings + var ireturnCfg *config.IreturnSettings var goModDirectivesCfg *config.GoModDirectivesSettings var tagliatelleCfg *config.TagliatelleSettings var gosecCfg *config.GoSecSettings @@ -131,6 +132,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { reviveCfg = &m.cfg.LintersSettings.Revive cyclopCfg = &m.cfg.LintersSettings.Cyclop importAsCfg = &m.cfg.LintersSettings.ImportAs + ireturnCfg = &m.cfg.LintersSettings.Ireturn goModDirectivesCfg = &m.cfg.LintersSettings.GoModDirectives tagliatelleCfg = &m.cfg.LintersSettings.Tagliatelle gosecCfg = &m.cfg.LintersSettings.Gosec @@ -456,6 +458,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithSince("v1.36.0"). WithPresets(linter.PresetStyle). WithURL("https://github.com/esimonov/ifshort"), + + linter.NewConfig(golinters.NewIreturn(ireturnCfg)). + WithSince("v1.43.0"). + WithPresets(linter.PresetStyle). + WithURL("https://github.com/butuzov/ireturn"), + linter.NewConfig(golinters.NewPredeclared(predeclaredCfg)). WithSince("v1.35.0"). WithPresets(linter.PresetStyle). diff --git a/test/testdata/ireturn_allow.go b/test/testdata/ireturn_allow.go new file mode 100644 index 000000000000..2368923a49ba --- /dev/null +++ b/test/testdata/ireturn_allow.go @@ -0,0 +1,13 @@ +// args: -Eireturn +// config: linters-settings.ireturn.allow=["IreturnAllowDoer"] +package testdata + +type ( + IreturnAllowDoer interface{ Do() } + ireturnAllowDoer struct{} +) + +func NewAllowDoer() IreturnAllowDoer { return new(ireturnAllowDoer) } +func (d *ireturnAllowDoer) Do() { /*...*/ } + +func NewerAllowDoer() *ireturnAllowDoer { return new(ireturnAllowDoer) } diff --git a/test/testdata/ireturn_default.go b/test/testdata/ireturn_default.go new file mode 100644 index 000000000000..0f9afd48f3a8 --- /dev/null +++ b/test/testdata/ireturn_default.go @@ -0,0 +1,12 @@ +// args: -Eireturn +package testdata + +type ( + IreturnDoer interface{ Do() } + ireturnDoer struct{} +) + +func New() IreturnDoer { return new(ireturnDoer) } // ERROR `New returns interface \(command-line-arguments.IreturnDoer\)` +func (d *ireturnDoer) Do() { /*...*/ } + +func Newer() *ireturnDoer { return new(ireturnDoer) } From ab0f00b4412e59e4317159f729c39f7b255f87db Mon Sep 17 00:00:00 2001 From: Oleg Butuzov Date: Sun, 5 Sep 2021 14:33:42 +0300 Subject: [PATCH 02/12] fix: removed local replace directive --- go.mod | 2 -- 1 file changed, 2 deletions(-) diff --git a/go.mod b/go.mod index 46aa3ce23452..de5e6c5a063c 100644 --- a/go.mod +++ b/go.mod @@ -93,5 +93,3 @@ require ( mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b // indirect mvdan.cc/unparam v0.0.0-20210104141923-aac4ce9116a7 ) - -replace github.com/butuzov/ireturn => ../ireturn From b8541fd5998fa2da746a8a9acd553179b7b2dcab Mon Sep 17 00:00:00 2001 From: Oleg Butuzov Date: Sun, 5 Sep 2021 14:39:28 +0300 Subject: [PATCH 03/12] fix: ran `go mod tidy` --- go.mod | 2 +- go.sum | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/go.mod b/go.mod index de5e6c5a063c..f664c0259e6a 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/ashanbrown/makezero v0.0.0-20210520155254-b6261585ddde github.com/bkielbasa/cyclop v1.2.0 github.com/bombsimon/wsl/v3 v3.3.0 - github.com/butuzov/ireturn v0.0.0-20210903145654-645c1cbdbffd // indirect + github.com/butuzov/ireturn v0.0.0-20210903145654-645c1cbdbffd github.com/charithe/durationcheck v0.0.8 github.com/daixiang0/gci v0.2.9 github.com/denis-tingajkin/go-header v0.4.2 diff --git a/go.sum b/go.sum index c9433203fbb7..c76dcf4f32b8 100644 --- a/go.sum +++ b/go.sum @@ -280,7 +280,6 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= From 4f5ed30ad3c03c354e867befd308bb283ff2f1d5 Mon Sep 17 00:00:00 2001 From: Oleg Butuzov Date: Mon, 6 Sep 2021 03:56:08 +0300 Subject: [PATCH 04/12] fix: moving linter to the end of the list --- pkg/lint/lintersdb/manager.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 819312d53c6f..6e043bdb29ac 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -458,12 +458,6 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithSince("v1.36.0"). WithPresets(linter.PresetStyle). WithURL("https://github.com/esimonov/ifshort"), - - linter.NewConfig(golinters.NewIreturn(ireturnCfg)). - WithSince("v1.43.0"). - WithPresets(linter.PresetStyle). - WithURL("https://github.com/butuzov/ireturn"), - linter.NewConfig(golinters.NewPredeclared(predeclaredCfg)). WithSince("v1.35.0"). WithPresets(linter.PresetStyle). @@ -514,6 +508,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithLoadForGoAnalysis(). WithURL("https://github.com/Antonboom/errname"). WithSince("v1.42.0"), + linter.NewConfig(golinters.NewIreturn(ireturnCfg)). + WithSince("v1.43.0"). + WithPresets(linter.PresetStyle). + WithURL("https://github.com/butuzov/ireturn"), // nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives linter.NewConfig(golinters.NewNoLintLint()). From fa4d2e0dd2be36eef5550bf5fb7edf7f4f168efb Mon Sep 17 00:00:00 2001 From: Oleg Butuzov Date: Mon, 6 Sep 2021 04:03:03 +0300 Subject: [PATCH 05/12] fixed: moved config to external file --- test/testdata/configs/ireturn.yml | 4 ++++ test/testdata/ireturn_allow.go | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 test/testdata/configs/ireturn.yml diff --git a/test/testdata/configs/ireturn.yml b/test/testdata/configs/ireturn.yml new file mode 100644 index 000000000000..05459dd7af73 --- /dev/null +++ b/test/testdata/configs/ireturn.yml @@ -0,0 +1,4 @@ +linters-settings: + ireturn: + allow: + - IreturnAllowDoer diff --git a/test/testdata/ireturn_allow.go b/test/testdata/ireturn_allow.go index 2368923a49ba..d6b968a2e244 100644 --- a/test/testdata/ireturn_allow.go +++ b/test/testdata/ireturn_allow.go @@ -1,5 +1,5 @@ // args: -Eireturn -// config: linters-settings.ireturn.allow=["IreturnAllowDoer"] +// config_path: testdata/configs/ireturn.yml package testdata type ( From 19386738eaef08bcc09078f536225d46a610bd39 Mon Sep 17 00:00:00 2001 From: Oleg Butuzov Date: Mon, 6 Sep 2021 04:04:15 +0300 Subject: [PATCH 06/12] docs: Apply suggestions from code review Co-authored-by: Ludovic Fernandez --- .golangci.example.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.golangci.example.yml b/.golangci.example.yml index 34033fa565ac..6ce7418518ed 100644 --- a/.golangci.example.yml +++ b/.golangci.example.yml @@ -448,14 +448,14 @@ linters-settings: alias: $1$2 ireturn: - # ireturn allows to use either `allow` or `reject` settings at the same time. - # both settings are a lists of the keywords (`empty` for interface{}`, - # `error` for errors, `stdlib` for standard library, `anon` for anonymouse - # interfaces ) and reglar expresssions matched to interfaces or packages. + # ireturn allows using either `allow` or `reject` settings at the same time. + # both settings are lists of the keywords (`empty` for interface{}`, + # `error` for errors, `stdlib` for standard library, `anon` for anonymous + # interfaces ) and regular expressions matched to interfaces or packages. allow: - # default settings are allow to use errors, empty interfaces, anonymouse - # interfaces, and interfaces provided by standard library. + # default settings are allowed to use errors, empty interfaces, anonymous + # interfaces, and interfaces provided by the standard library. - anon - error - empty From ccafd04873e06f5af291c74bfd58da8b10f37554 Mon Sep 17 00:00:00 2001 From: Oleg Butuzov Date: Mon, 6 Sep 2021 05:19:19 +0300 Subject: [PATCH 07/12] chores: version bump for `ireturn` --- go.mod | 2 +- go.sum | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index f664c0259e6a..e2ce280272ab 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/ashanbrown/makezero v0.0.0-20210520155254-b6261585ddde github.com/bkielbasa/cyclop v1.2.0 github.com/bombsimon/wsl/v3 v3.3.0 - github.com/butuzov/ireturn v0.0.0-20210903145654-645c1cbdbffd + github.com/butuzov/ireturn v0.1.0 github.com/charithe/durationcheck v0.0.8 github.com/daixiang0/gci v0.2.9 github.com/denis-tingajkin/go-header v0.4.2 diff --git a/go.sum b/go.sum index c76dcf4f32b8..9c92c0c175f6 100644 --- a/go.sum +++ b/go.sum @@ -95,8 +95,8 @@ github.com/bkielbasa/cyclop v1.2.0 h1:7Jmnh0yL2DjKfw28p86YTd/B4lRGcNuu12sKE35sM7 github.com/bkielbasa/cyclop v1.2.0/go.mod h1:qOI0yy6A7dYC4Zgsa72Ppm9kONl0RoIlPbzot9mhmeI= github.com/bombsimon/wsl/v3 v3.3.0 h1:Mka/+kRLoQJq7g2rggtgQsjuI/K5Efd87WX96EWFxjM= github.com/bombsimon/wsl/v3 v3.3.0/go.mod h1:st10JtZYLE4D5sC7b8xV4zTKZwAQjCH/Hy2Pm1FNZIc= -github.com/butuzov/ireturn v0.0.0-20210903145654-645c1cbdbffd h1:FCBvjaeDPGFMZpUeUrEikCrhL/Bp9UOUfhRTSBrkcgA= -github.com/butuzov/ireturn v0.0.0-20210903145654-645c1cbdbffd/go.mod h1:pxsIfiT9YhERToguQDHMdthS36WZBCltDcoh5dpbd2M= +github.com/butuzov/ireturn v0.1.0 h1:fMNgwuKMwsV9qtPNFgI7/NUOF3+CfbdLPGX6ZhDaMgA= +github.com/butuzov/ireturn v0.1.0/go.mod h1:Wh6Zl3IMtTpaIKbmwzqi6olnM9ptYQxxVacMsOEFPoc= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= @@ -280,9 +280,8 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= -github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= From 0b6951449bc17d4700031b175a17d4ee3c6bd97d Mon Sep 17 00:00:00 2001 From: Oleg Butuzov Date: Mon, 6 Sep 2021 10:31:54 +0300 Subject: [PATCH 08/12] Update pkg/config/linters_settings.go Co-authored-by: Ludovic Fernandez --- pkg/config/linters_settings.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index 94fd9114643d..201a57e2538d 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -189,7 +189,7 @@ type ExhaustiveStructSettings struct { type IreturnSettings struct { Allow []string `mapstructure:"allow"` - Reject []string `mapstructure:"return"` + Reject []string `mapstructure:"reject"` } type ForbidigoSettings struct { From 381e9e5986c7c8d1a388f7bf5f0f4b0328223927 Mon Sep 17 00:00:00 2001 From: Oleg Butuzov Date: Tue, 7 Sep 2021 05:51:57 +0300 Subject: [PATCH 09/12] fix: added `WithLoadForGoAnalysis()` to support standard library + test --- pkg/lint/lintersdb/manager.go | 1 + test/testdata/configs/ireturn_stdlib_reject.yml | 0 test/testdata/ireturn_reject_stdlib.go | 13 +++++++++++++ 3 files changed, 14 insertions(+) create mode 100644 test/testdata/configs/ireturn_stdlib_reject.yml create mode 100644 test/testdata/ireturn_reject_stdlib.go diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 6e043bdb29ac..93abb3bb1f30 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -511,6 +511,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { linter.NewConfig(golinters.NewIreturn(ireturnCfg)). WithSince("v1.43.0"). WithPresets(linter.PresetStyle). + WithLoadForGoAnalysis(). WithURL("https://github.com/butuzov/ireturn"), // nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives diff --git a/test/testdata/configs/ireturn_stdlib_reject.yml b/test/testdata/configs/ireturn_stdlib_reject.yml new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/test/testdata/ireturn_reject_stdlib.go b/test/testdata/ireturn_reject_stdlib.go new file mode 100644 index 000000000000..030f44fe51cd --- /dev/null +++ b/test/testdata/ireturn_reject_stdlib.go @@ -0,0 +1,13 @@ +// args: -Eireturn +// config_path: testdata/configs/ireturn_stdlib_reject.yml +package testdata + +import ( + "bytes" + "io" +) + +func NewAllowDoer() io.Writer { + var buf bytes.Buffer + return &buf +} From 286471ff7f812476aff2c3c89df46be779478b33 Mon Sep 17 00:00:00 2001 From: Oleg Butuzov Date: Tue, 7 Sep 2021 05:57:00 +0300 Subject: [PATCH 10/12] test: added stdlib test --- test/testdata/configs/ireturn_stdlib_reject.yml | 4 ++++ test/testdata/ireturn_reject_stdlib.go | 17 ++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/test/testdata/configs/ireturn_stdlib_reject.yml b/test/testdata/configs/ireturn_stdlib_reject.yml index e69de29bb2d1..c5e687f7c3d4 100644 --- a/test/testdata/configs/ireturn_stdlib_reject.yml +++ b/test/testdata/configs/ireturn_stdlib_reject.yml @@ -0,0 +1,4 @@ +linters-settings: + ireturn: + reject: + - stdlib diff --git a/test/testdata/ireturn_reject_stdlib.go b/test/testdata/ireturn_reject_stdlib.go index 030f44fe51cd..d57d81639904 100644 --- a/test/testdata/ireturn_reject_stdlib.go +++ b/test/testdata/ireturn_reject_stdlib.go @@ -7,7 +7,22 @@ import ( "io" ) -func NewAllowDoer() io.Writer { +func NewWriter() io.Writer { // ERROR `NewWriter returns interface \(io.Writer\)` var buf bytes.Buffer return &buf } + +func TestError() error { + return nil +} + +type Foo interface { + Foo() +} +type foo int + +func (f foo) Foo() {} + +func NewFoo() Foo { + return foo(1) +} From 0620021c5cac49d21afdfd40192525abedc366ca Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Tue, 7 Sep 2021 23:36:16 +0200 Subject: [PATCH 11/12] review --- .golangci.example.yml | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/.golangci.example.yml b/.golangci.example.yml index 6ce7418518ed..8c4c43ca764a 100644 --- a/.golangci.example.yml +++ b/.golangci.example.yml @@ -448,14 +448,17 @@ linters-settings: alias: $1$2 ireturn: - # ireturn allows using either `allow` or `reject` settings at the same time. - # both settings are lists of the keywords (`empty` for interface{}`, - # `error` for errors, `stdlib` for standard library, `anon` for anonymous - # interfaces ) and regular expressions matched to interfaces or packages. - + # ireturn allows using `allow` and `reject` settings at the same time. + # Both settings are lists of the keywords and regular expressions matched to interface or package names. + # keywords: + # - `empty` for `interface{}` + # - `error` for errors + # - `stdlib` for standard library + # - `anon` for anonymous interfaces + + # By default, it allows using errors, empty interfaces, anonymous interfaces, + # and interfaces provided by the standard library. allow: - # default settings are allowed to use errors, empty interfaces, anonymous - # interfaces, and interfaces provided by the standard library. - anon - error - empty @@ -463,8 +466,7 @@ linters-settings: # You can specify idiomatic endings for interface - (or|er)$ - - # or you can specify reject patterns + # Reject patterns reject: - github.com\/user\/package\/v4\.Type From 0f1cd166867dd4be3df0c1534fd4ea1f57e86470 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Wed, 8 Sep 2021 00:04:28 +0200 Subject: [PATCH 12/12] review --- pkg/golinters/ireturn.go | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/pkg/golinters/ireturn.go b/pkg/golinters/ireturn.go index c26b65059aef..3b5df66dae81 100644 --- a/pkg/golinters/ireturn.go +++ b/pkg/golinters/ireturn.go @@ -13,23 +13,18 @@ import ( func NewIreturn(settings *config.IreturnSettings) *goanalysis.Linter { a := analyzer.NewAnalyzer() + cfg := map[string]map[string]interface{}{} + if settings != nil { + cfg[a.Name] = map[string]interface{}{ + "allow": strings.Join(settings.Allow, ","), + "reject": strings.Join(settings.Reject, ","), + } + } + return goanalysis.NewLinter( a.Name, a.Doc, []*analysis.Analyzer{a}, - ireturnSettings(a.Name, settings), + cfg, ).WithLoadMode(goanalysis.LoadModeTypesInfo) } - -func ireturnSettings(name string, s *config.IreturnSettings) map[string]map[string]interface{} { - if s == nil { - return nil - } - - return map[string]map[string]interface{}{ - name: { - "allow": strings.Join(s.Allow, ","), - "reject": strings.Join(s.Reject, ","), - }, - } -}