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 19 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
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
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
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.0
github.com/go-critic/go-critic v0.6.2
github.com/go-xmlfmt/xmlfmt v0.0.0-20191208150333-d5b6f63a941b
Expand Down Expand Up @@ -162,7 +163,7 @@ require (
golang.org/x/exp/typeparams v0.0.0-20220218215828-6cf2b201936e // indirect
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 // indirect
golang.org/x/sys v0.0.0-20220403020550-483a9cbc67c0 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/protobuf v1.27.1 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum

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

17 changes: 17 additions & 0 deletions pkg/golinters/nonamedreturns.go
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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`