Skip to content

Commit 68ce706

Browse files
committed
nlreturn: add block-size option
adds configuration `block-size` that defines a size of the block (including return statement that is still "OK") so no return split required.
1 parent 3229262 commit 68ce706

File tree

6 files changed

+52
-7
lines changed

6 files changed

+52
-7
lines changed

.golangci.example.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ linters-settings:
158158
# Exclude godoc examples from forbidigo checks. Default is true.
159159
exclude_godoc_examples: false
160160

161+
nlreturn:
162+
# size of the block (including return statement that are still "OK")
163+
# so no return split required.
164+
block-size: 1
165+
161166
funlen:
162167
lines: 60
163168
statements: 40

pkg/config/linters_settings.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ type LintersSettings struct {
119119
Nakedret NakedretSettings
120120
Nestif NestifSettings
121121
NilNil NilNilSettings
122+
Nlreturn NlreturnSettings
122123
NoLintLint NoLintLintSettings
123124
Prealloc PreallocSettings
124125
Predeclared PredeclaredSettings
@@ -365,6 +366,10 @@ type NilNilSettings struct {
365366
CheckedTypes []string `mapstructure:"checked-types"`
366367
}
367368

369+
type NlreturnSettings struct {
370+
BlockSize int `mapstructure:"block-size"`
371+
}
372+
368373
type NoLintLintSettings struct {
369374
RequireExplanation bool `mapstructure:"require-explanation"`
370375
AllowLeadingSpace bool `mapstructure:"allow-leading-space"`

pkg/golinters/nlreturn.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,24 @@ import (
44
"github.com/ssgreg/nlreturn/v2/pkg/nlreturn"
55
"golang.org/x/tools/go/analysis"
66

7+
"github.com/golangci/golangci-lint/pkg/config"
78
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
89
)
910

10-
func NewNLReturn() *goanalysis.Linter {
11+
func NewNLReturn(settings *config.NlreturnSettings) *goanalysis.Linter {
12+
a := nlreturn.NewAnalyzer()
13+
14+
cfg := map[string]map[string]interface{}{}
15+
if settings != nil {
16+
cfg[a.Name] = map[string]interface{}{
17+
"block-size": settings.BlockSize,
18+
}
19+
}
20+
1121
return goanalysis.NewLinter(
12-
"nlreturn",
22+
a.Name,
1323
"nlreturn checks for a new line before return and branch statements to increase code clarity",
14-
[]*analysis.Analyzer{
15-
nlreturn.NewAnalyzer(),
16-
},
17-
nil,
24+
[]*analysis.Analyzer{a},
25+
cfg,
1826
).WithLoadMode(goanalysis.LoadModeSyntax)
1927
}

pkg/lint/lintersdb/manager.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
120120
var thelperCfg *config.ThelperSettings
121121
var unusedCfg *config.StaticCheckSettings
122122
var wrapcheckCfg *config.WrapcheckSettings
123+
var nlreturnCfg *config.NlreturnSettings
123124

124125
if m.cfg != nil {
125126
cyclopCfg = &m.cfg.LintersSettings.Cyclop
@@ -143,6 +144,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
143144
thelperCfg = &m.cfg.LintersSettings.Thelper
144145
unusedCfg = &m.cfg.LintersSettings.Unused
145146
wrapcheckCfg = &m.cfg.LintersSettings.Wrapcheck
147+
nlreturnCfg = &m.cfg.LintersSettings.Nlreturn
146148
}
147149

148150
const megacheckName = "megacheck"
@@ -414,7 +416,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
414416
WithPresets(linter.PresetBugs, linter.PresetSQL).
415417
WithLoadForGoAnalysis().
416418
WithURL("https://github.com/ryanrolds/sqlclosecheck"),
417-
linter.NewConfig(golinters.NewNLReturn()).
419+
linter.NewConfig(golinters.NewNLReturn(nlreturnCfg)).
418420
WithSince("v1.30.0").
419421
WithPresets(linter.PresetStyle).
420422
WithURL("https://github.com/ssgreg/nlreturn"),

test/testdata/configs/nlreturn.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
linters-settings:
2+
nlreturn:
3+
block-size: 2

test/testdata/nlreturn-block-size.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// args: -Enlreturn
2+
// config_path: testdata/configs/nlreturn.yml
3+
package testdata
4+
5+
func foo0(n int) int {
6+
if n == 1 {
7+
n2 := n * n
8+
return n2
9+
}
10+
11+
return 1
12+
}
13+
14+
func foo1(n int) int {
15+
if n == 1 {
16+
n2 := n * n
17+
n3 := n2 * n
18+
return n3 // ERROR "return with no blank line before"
19+
}
20+
21+
return 1
22+
}

0 commit comments

Comments
 (0)