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

Add nonamedreturns linter #2701

Merged
merged 21 commits into from Apr 17, 2022
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
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`