Skip to content

Commit

Permalink
nlreturn: add block-size option (golangci#2237)
Browse files Browse the repository at this point in the history
  • Loading branch information
butuzov authored and SeigeC committed Apr 4, 2023
1 parent 37b21b7 commit 7a07c91
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go.sum

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

5 changes: 5 additions & 0 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ type LintersSettings struct {
Nakedret NakedretSettings
Nestif NestifSettings
NilNil NilNilSettings
Nlreturn NlreturnSettings
NoLintLint NoLintLintSettings
Prealloc PreallocSettings
Predeclared PredeclaredSettings
Expand Down Expand Up @@ -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"`
Expand Down
20 changes: 14 additions & 6 deletions pkg/golinters/nlreturn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 3 additions & 1 deletion pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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"),
Expand Down
3 changes: 3 additions & 0 deletions test/testdata/configs/nlreturn.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
linters-settings:
nlreturn:
block-size: 2
22 changes: 22 additions & 0 deletions test/testdata/nlreturn-block-size.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 7a07c91

Please sign in to comment.