Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nlreturn: add block-size option #2237

Merged
merged 4 commits into from
Sep 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
}