Skip to content

Commit

Permalink
Add nonamedreturns linter (#2701)
Browse files Browse the repository at this point in the history
  • Loading branch information
firefart committed Apr 17, 2022
1 parent 62ab410 commit 333187c
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .golangci.example.yml
Expand Up @@ -790,6 +790,7 @@ linters-settings:
- lostcancel
- nilfunc
- nilness
- nonamedreturns
- printf
- reflectvaluecompare
- shadow
Expand Down Expand Up @@ -833,6 +834,7 @@ linters-settings:
- lostcancel
- nilfunc
- nilness
- nonamedreturns
- printf
- reflectvaluecompare
- shadow
Expand Down
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -23,6 +23,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.0
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
2 changes: 2 additions & 0 deletions go.sum

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

2 changes: 1 addition & 1 deletion pkg/golinters/godot.go
Expand Up @@ -37,7 +37,7 @@ func NewGodot() *goanalysis.Linter {

// Convert deprecated setting
// todo(butuzov): remove on v2 release
if cfg.CheckAll { // nolint:staticcheck
if cfg.CheckAll { // nolint:staticcheck // Keep for retro-compatibility.
settings.Scope = godot.AllScope
}

Expand Down
17 changes: 17 additions & 0 deletions pkg/golinters/nonamedreturns.go
@@ -0,0 +1,17 @@
package golinters

import (
"github.com/firefart/nonamedreturns/analyzer"
"golang.org/x/tools/go/analysis"

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

func NewNoNamedReturns() *goanalysis.Linter {
return goanalysis.NewLinter(
"nonamedreturns",
"Reports all named returns",
[]*analysis.Analyzer{analyzer.Analyzer},
nil,
).WithLoadMode(goanalysis.LoadModeSyntax)
}
5 changes: 5 additions & 0 deletions pkg/lint/lintersdb/manager.go
Expand Up @@ -526,6 +526,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithURL("https://github.com/sonatard/noctx").
WithNoopFallback(m.cfg),

linter.NewConfig(golinters.NewNoNamedReturns()).
WithSince("v1.46.0").
WithPresets(linter.PresetStyle).
WithURL("https://github.com/firefart/nonamedreturns"),

linter.NewConfig(golinters.NewParallelTest()).
WithSince("v1.33.0").
WithPresets(linter.PresetStyle, linter.PresetTest).
Expand Down
94 changes: 94 additions & 0 deletions test/testdata/nonamedreturns.go
@@ -0,0 +1,94 @@
//args: -Enonamedreturns
package testdata

import "fmt"

type asdf struct {
test string
}

func noParams() {
return
}

var c = func() {
return
}

var d = func() error {
return nil
}

var e = func() (err error) { // ERROR `named return "err" with type "error" found`
err = nil
return
}

var (
f = func() {
return
}

g = func() error {
return nil
}

h = func() (err error) { // ERROR `named return "err" with type "error" found`
err = nil
return
}
)

// this should not match as the implementation does not need named parameters (see below)
type funcDefintion func(arg1, arg2 interface{}) (num int, err error)

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

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

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

func test() {
a := funcVar()
_ = a

var function funcDefintion
function = funcDefintionImpl
i, err := function("", "")
_ = i
_ = err
function = funcDefintionImpl2
i, err = function("", "")
_ = i
_ = err
}

func good(i string) string {
return i
}

func bad(i string, a, b int) (ret1 string, ret2 interface{}, ret3, ret4 int, ret5 asdf) { // ERROR `named return "ret1" with type "string" found`
x := "dummy"
return fmt.Sprintf("%s", x), nil, 1, 2, asdf{}
}

func bad2() (msg string, err error) { // ERROR `named return "msg" with type "string" found`
msg = ""
err = nil
return
}

func myLog(format string, args ...interface{}) {
return
}

type obj struct{}

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

0 comments on commit 333187c

Please sign in to comment.