nlreturn: add block-size option (#2237)

This commit is contained in:
Oleg Butuzov 2021-09-18 15:29:45 +03:00 committed by GitHub
parent 32292622f8
commit 17d24ebd67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 57 additions and 8 deletions

View File

@ -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

2
go.mod
View File

@ -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

4
go.sum generated
View File

@ -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=

View File

@ -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"`

View File

@ -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)
}

View File

@ -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"),

3
test/testdata/configs/nlreturn.yml vendored Normal file
View File

@ -0,0 +1,3 @@
linters-settings:
nlreturn:
block-size: 2

22
test/testdata/nlreturn-block-size.go vendored Normal file
View File

@ -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
}