diff --git a/.golangci.example.yml b/.golangci.example.yml index ce12ab0bf2f3..496cb5e485b2 100644 --- a/.golangci.example.yml +++ b/.golangci.example.yml @@ -506,6 +506,11 @@ linters-settings: - map - chan + nlreturn: + # size of the block (including return statement that is still "OK") + # so no return split required. + block-size: 1 + nolintlint: # Enable to ensure that nolint directives are all used. Default is true. allow-unused: false diff --git a/go.mod b/go.mod index db6d32e1f25d..93626f501a84 100644 --- a/go.mod +++ b/go.mod @@ -74,7 +74,7 @@ require ( github.com/spf13/cobra v1.2.1 github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.8.1 - github.com/ssgreg/nlreturn/v2 v2.1.0 + github.com/ssgreg/nlreturn/v2 v2.2.1 github.com/stretchr/testify v1.7.0 github.com/tdakkota/asciicheck v0.0.0-20200416200610-e657995f937b github.com/tetafro/godot v1.4.10 diff --git a/go.sum b/go.sum index 2682f888e8b8..ed597ae306bf 100644 --- a/go.sum +++ b/go.sum @@ -653,6 +653,10 @@ github.com/spf13/viper v1.8.1 h1:Kq1fyeebqsBfbjZj4EL7gj2IO0mMaiyjYUWcUsl2O44= github.com/spf13/viper v1.8.1/go.mod h1:o0Pch8wJ9BVSWGQMbra6iw0oQ5oktSIBaujf1rJH9Ns= github.com/ssgreg/nlreturn/v2 v2.1.0 h1:6/s4Rc49L6Uo6RLjhWZGBpWWjfzk2yrf1nIW8m4wgVA= github.com/ssgreg/nlreturn/v2 v2.1.0/go.mod h1:E/iiPB78hV7Szg2YfRgyIrk1AD6JVMTRkkxBiELzh2I= +github.com/ssgreg/nlreturn/v2 v2.2.1-0.20210917114810-3d6ba68b229e h1://7YGfVLvQOQ5CkrrpKj0hZO9EaH0fpcV7zTeNcV1Y0= +github.com/ssgreg/nlreturn/v2 v2.2.1-0.20210917114810-3d6ba68b229e/go.mod h1:E/iiPB78hV7Szg2YfRgyIrk1AD6JVMTRkkxBiELzh2I= +github.com/ssgreg/nlreturn/v2 v2.2.1 h1:X4XDI7jstt3ySqGU86YGAURbxw3oTDPK9sPEi6YEwQ0= +github.com/ssgreg/nlreturn/v2 v2.2.1/go.mod h1:E/iiPB78hV7Szg2YfRgyIrk1AD6JVMTRkkxBiELzh2I= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index b0d1d7cd7690..9ef297f9c35f 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -119,6 +119,7 @@ type LintersSettings struct { Nakedret NakedretSettings Nestif NestifSettings NilNil NilNilSettings + Nlreturn NlreturnSettings NoLintLint NoLintLintSettings Prealloc PreallocSettings Predeclared PredeclaredSettings @@ -365,6 +366,10 @@ type NilNilSettings struct { CheckedTypes []string `mapstructure:"checked-types"` } +type NlreturnSettings struct { + BlockSize int `mapstructure:"block-size"` +} + type NoLintLintSettings struct { RequireExplanation bool `mapstructure:"require-explanation"` AllowLeadingSpace bool `mapstructure:"allow-leading-space"` diff --git a/pkg/golinters/nlreturn.go b/pkg/golinters/nlreturn.go index 3b661c64c83c..fb4919f8a953 100644 --- a/pkg/golinters/nlreturn.go +++ b/pkg/golinters/nlreturn.go @@ -4,16 +4,24 @@ import ( "github.com/ssgreg/nlreturn/v2/pkg/nlreturn" "golang.org/x/tools/go/analysis" + "github.com/golangci/golangci-lint/pkg/config" "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" ) -func NewNLReturn() *goanalysis.Linter { +func NewNLReturn(settings *config.NlreturnSettings) *goanalysis.Linter { + a := nlreturn.NewAnalyzer() + + cfg := map[string]map[string]interface{}{} + if settings != nil { + cfg[a.Name] = map[string]interface{}{ + "block-size": settings.BlockSize, + } + } + return goanalysis.NewLinter( - "nlreturn", + a.Name, "nlreturn checks for a new line before return and branch statements to increase code clarity", - []*analysis.Analyzer{ - nlreturn.NewAnalyzer(), - }, - nil, + []*analysis.Analyzer{a}, + cfg, ).WithLoadMode(goanalysis.LoadModeSyntax) } diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 5e7fcd380cb8..ecc2efec97e3 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -120,6 +120,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { var thelperCfg *config.ThelperSettings var unusedCfg *config.StaticCheckSettings var wrapcheckCfg *config.WrapcheckSettings + var nlreturnCfg *config.NlreturnSettings if m.cfg != nil { cyclopCfg = &m.cfg.LintersSettings.Cyclop @@ -143,6 +144,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { thelperCfg = &m.cfg.LintersSettings.Thelper unusedCfg = &m.cfg.LintersSettings.Unused wrapcheckCfg = &m.cfg.LintersSettings.Wrapcheck + nlreturnCfg = &m.cfg.LintersSettings.Nlreturn } const megacheckName = "megacheck" @@ -414,7 +416,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithPresets(linter.PresetBugs, linter.PresetSQL). WithLoadForGoAnalysis(). WithURL("https://github.com/ryanrolds/sqlclosecheck"), - linter.NewConfig(golinters.NewNLReturn()). + linter.NewConfig(golinters.NewNLReturn(nlreturnCfg)). WithSince("v1.30.0"). WithPresets(linter.PresetStyle). WithURL("https://github.com/ssgreg/nlreturn"), diff --git a/test/testdata/configs/nlreturn.yml b/test/testdata/configs/nlreturn.yml new file mode 100644 index 000000000000..edb719386f54 --- /dev/null +++ b/test/testdata/configs/nlreturn.yml @@ -0,0 +1,3 @@ +linters-settings: + nlreturn: + block-size: 2 diff --git a/test/testdata/nlreturn-block-size.go b/test/testdata/nlreturn-block-size.go new file mode 100644 index 000000000000..5cf9563446b7 --- /dev/null +++ b/test/testdata/nlreturn-block-size.go @@ -0,0 +1,22 @@ +// args: -Enlreturn +// config_path: testdata/configs/nlreturn.yml +package testdata + +func foo0(n int) int { + if n == 1 { + n2 := n * n + return n2 + } + + return 1 +} + +func foo1(n int) int { + if n == 1 { + n2 := n * n + n3 := n2 * n + return n3 // ERROR "return with no blank line before" + } + + return 1 +}