Skip to content

Commit

Permalink
build(deps): bump github.com/firefart/nonamedreturns from 1.0.1 to 1.…
Browse files Browse the repository at this point in the history
…0.2 (#2929)

Co-authored-by: Fernandez Ludovic <ldez@users.noreply.github.com>
  • Loading branch information
dependabot[bot] and ldez committed Jun 19, 2022
1 parent ae2a968 commit 98c811d
Show file tree
Hide file tree
Showing 9 changed files with 291 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .golangci.reference.yml
Expand Up @@ -1154,6 +1154,11 @@ linters-settings:
# Default: false
require-specific: true

nonamedreturns:
# Do not complain about named error, if it is assigned inside defer.
# Default: false
allow-error-in-defer: true

paralleltest:
# Ignore missing calls to `t.Parallel()` and only report incorrect uses of it.
# Default: false
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -24,7 +24,7 @@ require (
github.com/denis-tingaikin/go-header v0.4.3
github.com/esimonov/ifshort v1.0.4
github.com/fatih/color v1.13.0
github.com/firefart/nonamedreturns v1.0.1
github.com/firefart/nonamedreturns v1.0.2
github.com/fzipp/gocyclo v0.5.1
github.com/go-critic/go-critic v0.6.3
github.com/go-xmlfmt/xmlfmt v0.0.0-20191208150333-d5b6f63a941b
Expand Down
4 changes: 2 additions & 2 deletions go.sum

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

4 changes: 4 additions & 0 deletions pkg/config/linters_settings.go
Expand Up @@ -158,6 +158,7 @@ type LintersSettings struct {
NilNil NilNilSettings
Nlreturn NlreturnSettings
NoLintLint NoLintLintSettings
NoNamedReturns NoNamedReturnsSettings
ParallelTest ParallelTestSettings
Prealloc PreallocSettings
Predeclared PredeclaredSettings
Expand Down Expand Up @@ -484,6 +485,9 @@ type NoLintLintSettings struct {
AllowUnused bool `mapstructure:"allow-unused"`
}

type NoNamedReturnsSettings struct {
AllowErrorInDefer bool `mapstructure:"allow-error-in-defer"`
}
type ParallelTestSettings struct {
IgnoreMissing bool `mapstructure:"ignore-missing"`
}
Expand Down
22 changes: 17 additions & 5 deletions pkg/golinters/nonamedreturns.go
Expand Up @@ -4,14 +4,26 @@ import (
"github.com/firefart/nonamedreturns/analyzer"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)

func NewNoNamedReturns() *goanalysis.Linter {
func NewNoNamedReturns(settings *config.NoNamedReturnsSettings) *goanalysis.Linter {
a := analyzer.Analyzer

var cfg map[string]map[string]interface{}
if settings != nil {
cfg = map[string]map[string]interface{}{
a.Name: {
analyzer.FlagAllowErrorInDefer: settings.AllowErrorInDefer,
},
}
}

return goanalysis.NewLinter(
"nonamedreturns",
"Reports all named returns",
[]*analysis.Analyzer{analyzer.Analyzer},
nil,
a.Name,
a.Doc,
[]*analysis.Analyzer{a},
cfg,
).WithLoadMode(goanalysis.LoadModeSyntax)
}
4 changes: 3 additions & 1 deletion pkg/lint/lintersdb/manager.go
Expand Up @@ -147,6 +147,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
nilNilCfg *config.NilNilSettings
nlreturnCfg *config.NlreturnSettings
noLintLintCfg *config.NoLintLintSettings
noNamedReturnsCfg *config.NoNamedReturnsSettings
parallelTestCfg *config.ParallelTestSettings
preallocCfg *config.PreallocSettings
predeclaredCfg *config.PredeclaredSettings
Expand Down Expand Up @@ -216,6 +217,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
nilNilCfg = &m.cfg.LintersSettings.NilNil
nlreturnCfg = &m.cfg.LintersSettings.Nlreturn
noLintLintCfg = &m.cfg.LintersSettings.NoLintLint
noNamedReturnsCfg = &m.cfg.LintersSettings.NoNamedReturns
preallocCfg = &m.cfg.LintersSettings.Prealloc
parallelTestCfg = &m.cfg.LintersSettings.ParallelTest
predeclaredCfg = &m.cfg.LintersSettings.Predeclared
Expand Down Expand Up @@ -623,7 +625,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithURL("https://github.com/sonatard/noctx").
WithNoopFallback(m.cfg),

linter.NewConfig(golinters.NewNoNamedReturns()).
linter.NewConfig(golinters.NewNoNamedReturns(noNamedReturnsCfg)).
WithSince("v1.46.0").
WithPresets(linter.PresetStyle).
WithURL("https://github.com/firefart/nonamedreturns"),
Expand Down
3 changes: 3 additions & 0 deletions test/testdata/configs/nonamedreturns.yml
@@ -0,0 +1,3 @@
linters-settings:
nonamedreturns:
allow-error-in-defer: true
32 changes: 31 additions & 1 deletion test/testdata/nonamedreturns.go
@@ -1,4 +1,4 @@
//args: -Enonamedreturns
// args: -Enonamedreturns
package testdata

import "fmt"
Expand All @@ -24,6 +24,17 @@ var e = func() (err error) { // ERROR `named return "err" with type "error" foun
return
}

var e2 = func() (_ error) {
return
}

func deferWithError() (err error) { // ERROR `named return "err" with type "error" found`
defer func() {
err = nil // use flag to allow this
}()
return
}

var (
f = func() {
return
Expand All @@ -37,6 +48,10 @@ var (
err = nil
return
}

h2 = func() (_ error) {
return
}
)

// this should not match as the implementation does not need named parameters (see below)
Expand All @@ -50,11 +65,24 @@ func funcDefintionImpl2(arg1, arg2 interface{}) (num int, err error) { // ERROR
return 0, nil
}

func funcDefintionImpl3(arg1, arg2 interface{}) (num int, _ error) { // ERROR `named return "num" with type "int" found`
return 0, nil
}

func funcDefintionImpl4(arg1, arg2 interface{}) (_ int, _ error) {
return 0, nil
}

var funcVar = func() (msg string) { // ERROR `named return "msg" with type "string" found`
msg = "c"
return msg
}

var funcVar2 = func() (_ string) {
msg := "c"
return msg
}

func test() {
a := funcVar()
_ = a
Expand Down Expand Up @@ -92,3 +120,5 @@ func myLog(format string, args ...interface{}) {
type obj struct{}

func (o *obj) func1() (err error) { return nil } // ERROR `named return "err" with type "error" found`

func (o *obj) func2() (_ error) { return nil }

0 comments on commit 98c811d

Please sign in to comment.