nlreturn: add block-size option (#2237)
This commit is contained in:
parent
32292622f8
commit
17d24ebd67
@ -506,6 +506,11 @@ linters-settings:
|
|||||||
- map
|
- map
|
||||||
- chan
|
- chan
|
||||||
|
|
||||||
|
nlreturn:
|
||||||
|
# size of the block (including return statement that is still "OK")
|
||||||
|
# so no return split required.
|
||||||
|
block-size: 1
|
||||||
|
|
||||||
nolintlint:
|
nolintlint:
|
||||||
# Enable to ensure that nolint directives are all used. Default is true.
|
# Enable to ensure that nolint directives are all used. Default is true.
|
||||||
allow-unused: false
|
allow-unused: false
|
||||||
|
2
go.mod
2
go.mod
@ -74,7 +74,7 @@ require (
|
|||||||
github.com/spf13/cobra v1.2.1
|
github.com/spf13/cobra v1.2.1
|
||||||
github.com/spf13/pflag v1.0.5
|
github.com/spf13/pflag v1.0.5
|
||||||
github.com/spf13/viper v1.8.1
|
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/stretchr/testify v1.7.0
|
||||||
github.com/tdakkota/asciicheck v0.0.0-20200416200610-e657995f937b
|
github.com/tdakkota/asciicheck v0.0.0-20200416200610-e657995f937b
|
||||||
github.com/tetafro/godot v1.4.10
|
github.com/tetafro/godot v1.4.10
|
||||||
|
4
go.sum
generated
4
go.sum
generated
@ -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/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 h1:6/s4Rc49L6Uo6RLjhWZGBpWWjfzk2yrf1nIW8m4wgVA=
|
||||||
github.com/ssgreg/nlreturn/v2 v2.1.0/go.mod h1:E/iiPB78hV7Szg2YfRgyIrk1AD6JVMTRkkxBiELzh2I=
|
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.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
|
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
|
||||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
@ -119,6 +119,7 @@ type LintersSettings struct {
|
|||||||
Nakedret NakedretSettings
|
Nakedret NakedretSettings
|
||||||
Nestif NestifSettings
|
Nestif NestifSettings
|
||||||
NilNil NilNilSettings
|
NilNil NilNilSettings
|
||||||
|
Nlreturn NlreturnSettings
|
||||||
NoLintLint NoLintLintSettings
|
NoLintLint NoLintLintSettings
|
||||||
Prealloc PreallocSettings
|
Prealloc PreallocSettings
|
||||||
Predeclared PredeclaredSettings
|
Predeclared PredeclaredSettings
|
||||||
@ -365,6 +366,10 @@ type NilNilSettings struct {
|
|||||||
CheckedTypes []string `mapstructure:"checked-types"`
|
CheckedTypes []string `mapstructure:"checked-types"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type NlreturnSettings struct {
|
||||||
|
BlockSize int `mapstructure:"block-size"`
|
||||||
|
}
|
||||||
|
|
||||||
type NoLintLintSettings struct {
|
type NoLintLintSettings struct {
|
||||||
RequireExplanation bool `mapstructure:"require-explanation"`
|
RequireExplanation bool `mapstructure:"require-explanation"`
|
||||||
AllowLeadingSpace bool `mapstructure:"allow-leading-space"`
|
AllowLeadingSpace bool `mapstructure:"allow-leading-space"`
|
||||||
|
@ -4,16 +4,24 @@ import (
|
|||||||
"github.com/ssgreg/nlreturn/v2/pkg/nlreturn"
|
"github.com/ssgreg/nlreturn/v2/pkg/nlreturn"
|
||||||
"golang.org/x/tools/go/analysis"
|
"golang.org/x/tools/go/analysis"
|
||||||
|
|
||||||
|
"github.com/golangci/golangci-lint/pkg/config"
|
||||||
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
|
"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(
|
return goanalysis.NewLinter(
|
||||||
"nlreturn",
|
a.Name,
|
||||||
"nlreturn checks for a new line before return and branch statements to increase code clarity",
|
"nlreturn checks for a new line before return and branch statements to increase code clarity",
|
||||||
[]*analysis.Analyzer{
|
[]*analysis.Analyzer{a},
|
||||||
nlreturn.NewAnalyzer(),
|
cfg,
|
||||||
},
|
|
||||||
nil,
|
|
||||||
).WithLoadMode(goanalysis.LoadModeSyntax)
|
).WithLoadMode(goanalysis.LoadModeSyntax)
|
||||||
}
|
}
|
||||||
|
@ -120,6 +120,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
|
|||||||
var thelperCfg *config.ThelperSettings
|
var thelperCfg *config.ThelperSettings
|
||||||
var unusedCfg *config.StaticCheckSettings
|
var unusedCfg *config.StaticCheckSettings
|
||||||
var wrapcheckCfg *config.WrapcheckSettings
|
var wrapcheckCfg *config.WrapcheckSettings
|
||||||
|
var nlreturnCfg *config.NlreturnSettings
|
||||||
|
|
||||||
if m.cfg != nil {
|
if m.cfg != nil {
|
||||||
cyclopCfg = &m.cfg.LintersSettings.Cyclop
|
cyclopCfg = &m.cfg.LintersSettings.Cyclop
|
||||||
@ -143,6 +144,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
|
|||||||
thelperCfg = &m.cfg.LintersSettings.Thelper
|
thelperCfg = &m.cfg.LintersSettings.Thelper
|
||||||
unusedCfg = &m.cfg.LintersSettings.Unused
|
unusedCfg = &m.cfg.LintersSettings.Unused
|
||||||
wrapcheckCfg = &m.cfg.LintersSettings.Wrapcheck
|
wrapcheckCfg = &m.cfg.LintersSettings.Wrapcheck
|
||||||
|
nlreturnCfg = &m.cfg.LintersSettings.Nlreturn
|
||||||
}
|
}
|
||||||
|
|
||||||
const megacheckName = "megacheck"
|
const megacheckName = "megacheck"
|
||||||
@ -414,7 +416,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
|
|||||||
WithPresets(linter.PresetBugs, linter.PresetSQL).
|
WithPresets(linter.PresetBugs, linter.PresetSQL).
|
||||||
WithLoadForGoAnalysis().
|
WithLoadForGoAnalysis().
|
||||||
WithURL("https://github.com/ryanrolds/sqlclosecheck"),
|
WithURL("https://github.com/ryanrolds/sqlclosecheck"),
|
||||||
linter.NewConfig(golinters.NewNLReturn()).
|
linter.NewConfig(golinters.NewNLReturn(nlreturnCfg)).
|
||||||
WithSince("v1.30.0").
|
WithSince("v1.30.0").
|
||||||
WithPresets(linter.PresetStyle).
|
WithPresets(linter.PresetStyle).
|
||||||
WithURL("https://github.com/ssgreg/nlreturn"),
|
WithURL("https://github.com/ssgreg/nlreturn"),
|
||||||
|
3
test/testdata/configs/nlreturn.yml
vendored
Normal file
3
test/testdata/configs/nlreturn.yml
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
linters-settings:
|
||||||
|
nlreturn:
|
||||||
|
block-size: 2
|
22
test/testdata/nlreturn-block-size.go
vendored
Normal file
22
test/testdata/nlreturn-block-size.go
vendored
Normal 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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user