From 38fff6a43a80b8ebd72ff0ad5076d7f76dd2b7f4 Mon Sep 17 00:00:00 2001 From: Leon Klingele Date: Thu, 27 Jan 2022 18:12:02 +0100 Subject: [PATCH] Add "funcresult" linter --- .golangci.example.yml | 10 ++++++++++ go.mod | 4 +++- go.sum | 8 ++++++-- pkg/config/linters_settings.go | 13 +++++++++++++ pkg/golinters/funcresult.go | 26 ++++++++++++++++++++++++++ pkg/lint/lintersdb/manager.go | 7 +++++++ 6 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 pkg/golinters/funcresult.go diff --git a/.golangci.example.yml b/.golangci.example.yml index 0e17ee939807..dd508d6a6d2a 100644 --- a/.golangci.example.yml +++ b/.golangci.example.yml @@ -284,6 +284,14 @@ linters-settings: # Default: true exclude_godoc_examples: false + funcresult: + # Require the use of named function result parameters only. + # Default: false + require-named: true + # Require the use of unnamed function result parameters only. + # Default: false + require-unnamed: true + funlen: # Checks the number of lines in a function. # If lower than 0, disable the check. @@ -1561,6 +1569,7 @@ linters: - exportloopref - forbidigo - forcetypeassert + - funcresult - funlen - gci - gochecknoglobals @@ -1650,6 +1659,7 @@ linters: - exportloopref - forbidigo - forcetypeassert + - funcresult - funlen - gci - gochecknoglobals diff --git a/go.mod b/go.mod index 01a1286bffd4..d21b6f3d14a4 100644 --- a/go.mod +++ b/go.mod @@ -50,6 +50,7 @@ require ( github.com/kyoh86/exportloopref v0.1.8 github.com/ldez/gomoddirectives v0.2.2 github.com/ldez/tagliatelle v0.3.0 + github.com/leonklingele/funcresult v1.0.0 github.com/leonklingele/grouper v1.1.0 github.com/maratori/testpackage v1.0.1 github.com/matoous/godox v0.0.0-20210227103229-6504466cf951 // v1.0 @@ -96,7 +97,8 @@ require ( github.com/yagipy/maintidx v1.0.0 github.com/yeya24/promlinter v0.1.1-0.20210918184747-d757024714a1 gitlab.com/bosi/decorder v0.2.1 - golang.org/x/tools v0.1.9-0.20211228192929-ee1ca4ffc4da + golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect + golang.org/x/tools v0.1.9 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b honnef.co/go/tools v0.2.2 mvdan.cc/gofumpt v0.2.1 diff --git a/go.sum b/go.sum index fadae35e04d9..f27b0f3ad0c4 100644 --- a/go.sum +++ b/go.sum @@ -489,6 +489,8 @@ github.com/ldez/gomoddirectives v0.2.2 h1:p9/sXuNFArS2RLc+UpYZSI4KQwGMEDWC/LbtF5 github.com/ldez/gomoddirectives v0.2.2/go.mod h1:cpgBogWITnCfRq2qGoDkKMEVSaarhdBr6g8G04uz6d0= github.com/ldez/tagliatelle v0.3.0 h1:Aubm2ZsrsjIGFvdxemMPJaXrSJ5Cys6VWyTQFt9k2dI= github.com/ldez/tagliatelle v0.3.0/go.mod h1:8s6WJQwEYHbKZDsp/LjArytKOG8qaMrKQQ3mFukHs88= +github.com/leonklingele/funcresult v1.0.0 h1:+Gy+WmWqYAXFHk2r+UrQ7WDeNDxBw/aN7vBhJh9Z3vQ= +github.com/leonklingele/funcresult v1.0.0/go.mod h1:d0V6bJI7+NSDfYyeIC6aKu/gnZwq77UWT/poJgf9xPo= github.com/leonklingele/grouper v1.1.0 h1:tC2y/ygPbMFSBOs3DcyaEMKnnwH7eYKzohOtRrf0SAg= github.com/leonklingele/grouper v1.1.0/go.mod h1:uk3I3uDfi9B6PeUjsCKi6ndcf63Uy7snXgR4yDYQVDY= github.com/letsencrypt/pkcs11key/v4 v4.0.0/go.mod h1:EFUvBDay26dErnNb70Nd0/VW3tJiIbETBPTl9ATXQag= @@ -1073,8 +1075,9 @@ golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211210111614-af8b64212486/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211213223007-03aa0b5f6827/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 h1:XfKQ4OlFl8okEOr5UvAqFRVj8pY/4yfcXrddB8qAbU0= +golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -1190,8 +1193,9 @@ golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo= golang.org/x/tools v0.1.8/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU= -golang.org/x/tools v0.1.9-0.20211228192929-ee1ca4ffc4da h1:Tno72dYE94v/7SyyIj9iBsc7OOjFu2PyNcl7yxxeZD8= golang.org/x/tools v0.1.9-0.20211228192929-ee1ca4ffc4da/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU= +golang.org/x/tools v0.1.9 h1:j9KsMiaP1c3B0OTQGth0/k+miLGTgLsAFUCrF2vLcF8= +golang.org/x/tools v0.1.9/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index 7e97ad3c5575..b4762ee97815 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -112,6 +112,7 @@ type LintersSettings struct { Exhaustive ExhaustiveSettings ExhaustiveStruct ExhaustiveStructSettings Forbidigo ForbidigoSettings + FuncResult FuncResultSettings Funlen FunlenSettings Gci GciSettings Gocognit GocognitSettings @@ -247,6 +248,18 @@ type ForbidigoSettings struct { ExcludeGodocExamples bool `mapstructure:"exclude-godoc-examples"` } +type FuncResultSettings struct { + RequireNamed bool `mapstructure:"require-named"` + RequireUnnamed bool `mapstructure:"require-unnamed"` +} + +func (cfg FuncResultSettings) Validate() error { + if cfg.RequireNamed && cfg.RequireUnnamed { + return errors.New("require-named and require-unnamed can't be combined") + } + return nil +} + type FunlenSettings struct { Lines int Statements int diff --git a/pkg/golinters/funcresult.go b/pkg/golinters/funcresult.go new file mode 100644 index 000000000000..795151be3365 --- /dev/null +++ b/pkg/golinters/funcresult.go @@ -0,0 +1,26 @@ +package golinters + +import ( + funcresult "github.com/leonklingele/funcresult/pkg/analyzer" + "golang.org/x/tools/go/analysis" + + "github.com/golangci/golangci-lint/pkg/config" + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" +) + +func NewFuncResult(settings *config.FuncResultSettings) *goanalysis.Linter { + linterCfg := map[string]map[string]interface{}{} + if settings != nil { + linterCfg["funcresult"] = map[string]interface{}{ + "require-named": settings.RequireNamed, + "require-unnamed": settings.RequireUnnamed, + } + } + + return goanalysis.NewLinter( + "funcresult", + "An analyzer to function result parameters.", + []*analysis.Analyzer{funcresult.New()}, + linterCfg, + ).WithLoadMode(goanalysis.LoadModeSyntax) +} diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index cf6a0f8d4b6f..4c7f647986c2 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -107,6 +107,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { var errorlintCfg *config.ErrorLintSettings var exhaustiveCfg *config.ExhaustiveSettings var exhaustiveStructCfg *config.ExhaustiveStructSettings + var funcResultCfg *config.FuncResultSettings var goModDirectivesCfg *config.GoModDirectivesSettings var goMndCfg *config.GoMndSettings var gosecCfg *config.GoSecSettings @@ -139,6 +140,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { errorlintCfg = &m.cfg.LintersSettings.ErrorLint exhaustiveCfg = &m.cfg.LintersSettings.Exhaustive exhaustiveStructCfg = &m.cfg.LintersSettings.ExhaustiveStruct + funcResultCfg = &m.cfg.LintersSettings.FuncResult goModDirectivesCfg = &m.cfg.LintersSettings.GoModDirectives goMndCfg = &m.cfg.LintersSettings.Gomnd gosecCfg = &m.cfg.LintersSettings.Gosec @@ -287,6 +289,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithPresets(linter.PresetStyle). WithURL("https://github.com/gostaticanalysis/forcetypeassert"), + linter.NewConfig(golinters.NewFuncResult(funcResultCfg)). + WithSince("v1.45.0"). + WithPresets(linter.PresetStyle). + WithURL("https://github.com/leonklingele/funcresult"), + linter.NewConfig(golinters.NewFunlen()). WithSince("v1.18.0"). WithPresets(linter.PresetComplexity).