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

Add "funcresult" linter #2526

Closed
Closed
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
10 changes: 10 additions & 0 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -1561,6 +1569,7 @@ linters:
- exportloopref
- forbidigo
- forcetypeassert
- funcresult
- funlen
- gci
- gochecknoglobals
Expand Down Expand Up @@ -1650,6 +1659,7 @@ linters:
- exportloopref
- forbidigo
- forcetypeassert
- funcresult
- funlen
- gci
- gochecknoglobals
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ type LintersSettings struct {
Exhaustive ExhaustiveSettings
ExhaustiveStruct ExhaustiveStructSettings
Forbidigo ForbidigoSettings
FuncResult FuncResultSettings
Funlen FunlenSettings
Gci GciSettings
Gocognit GocognitSettings
Expand Down Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions pkg/golinters/funcresult.go
Original file line number Diff line number Diff line change
@@ -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)
}
7 changes: 7 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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).
Expand Down