Skip to content

Commit 333187c

Browse files
authored
Add nonamedreturns linter (#2701)
1 parent 62ab410 commit 333187c

File tree

7 files changed

+122
-1
lines changed

7 files changed

+122
-1
lines changed

.golangci.example.yml

+2
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,7 @@ linters-settings:
790790
- lostcancel
791791
- nilfunc
792792
- nilness
793+
- nonamedreturns
793794
- printf
794795
- reflectvaluecompare
795796
- shadow
@@ -833,6 +834,7 @@ linters-settings:
833834
- lostcancel
834835
- nilfunc
835836
- nilness
837+
- nonamedreturns
836838
- printf
837839
- reflectvaluecompare
838840
- shadow

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ require (
2323
github.com/denis-tingaikin/go-header v0.4.3
2424
github.com/esimonov/ifshort v1.0.4
2525
github.com/fatih/color v1.13.0
26+
github.com/firefart/nonamedreturns v1.0.0
2627
github.com/fzipp/gocyclo v0.5.1
2728
github.com/go-critic/go-critic v0.6.3
2829
github.com/go-xmlfmt/xmlfmt v0.0.0-20191208150333-d5b6f63a941b

go.sum

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/godot.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func NewGodot() *goanalysis.Linter {
3737

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

pkg/golinters/nonamedreturns.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package golinters
2+
3+
import (
4+
"github.com/firefart/nonamedreturns/analyzer"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
8+
)
9+
10+
func NewNoNamedReturns() *goanalysis.Linter {
11+
return goanalysis.NewLinter(
12+
"nonamedreturns",
13+
"Reports all named returns",
14+
[]*analysis.Analyzer{analyzer.Analyzer},
15+
nil,
16+
).WithLoadMode(goanalysis.LoadModeSyntax)
17+
}

pkg/lint/lintersdb/manager.go

+5
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
526526
WithURL("https://github.com/sonatard/noctx").
527527
WithNoopFallback(m.cfg),
528528

529+
linter.NewConfig(golinters.NewNoNamedReturns()).
530+
WithSince("v1.46.0").
531+
WithPresets(linter.PresetStyle).
532+
WithURL("https://github.com/firefart/nonamedreturns"),
533+
529534
linter.NewConfig(golinters.NewParallelTest()).
530535
WithSince("v1.33.0").
531536
WithPresets(linter.PresetStyle, linter.PresetTest).

test/testdata/nonamedreturns.go

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//args: -Enonamedreturns
2+
package testdata
3+
4+
import "fmt"
5+
6+
type asdf struct {
7+
test string
8+
}
9+
10+
func noParams() {
11+
return
12+
}
13+
14+
var c = func() {
15+
return
16+
}
17+
18+
var d = func() error {
19+
return nil
20+
}
21+
22+
var e = func() (err error) { // ERROR `named return "err" with type "error" found`
23+
err = nil
24+
return
25+
}
26+
27+
var (
28+
f = func() {
29+
return
30+
}
31+
32+
g = func() error {
33+
return nil
34+
}
35+
36+
h = func() (err error) { // ERROR `named return "err" with type "error" found`
37+
err = nil
38+
return
39+
}
40+
)
41+
42+
// this should not match as the implementation does not need named parameters (see below)
43+
type funcDefintion func(arg1, arg2 interface{}) (num int, err error)
44+
45+
func funcDefintionImpl(arg1, arg2 interface{}) (int, error) {
46+
return 0, nil
47+
}
48+
49+
func funcDefintionImpl2(arg1, arg2 interface{}) (num int, err error) { // ERROR `named return "num" with type "int" found`
50+
return 0, nil
51+
}
52+
53+
var funcVar = func() (msg string) { // ERROR `named return "msg" with type "string" found`
54+
msg = "c"
55+
return msg
56+
}
57+
58+
func test() {
59+
a := funcVar()
60+
_ = a
61+
62+
var function funcDefintion
63+
function = funcDefintionImpl
64+
i, err := function("", "")
65+
_ = i
66+
_ = err
67+
function = funcDefintionImpl2
68+
i, err = function("", "")
69+
_ = i
70+
_ = err
71+
}
72+
73+
func good(i string) string {
74+
return i
75+
}
76+
77+
func bad(i string, a, b int) (ret1 string, ret2 interface{}, ret3, ret4 int, ret5 asdf) { // ERROR `named return "ret1" with type "string" found`
78+
x := "dummy"
79+
return fmt.Sprintf("%s", x), nil, 1, 2, asdf{}
80+
}
81+
82+
func bad2() (msg string, err error) { // ERROR `named return "msg" with type "string" found`
83+
msg = ""
84+
err = nil
85+
return
86+
}
87+
88+
func myLog(format string, args ...interface{}) {
89+
return
90+
}
91+
92+
type obj struct{}
93+
94+
func (o *obj) func1() (err error) { return nil } // ERROR `named return "err" with type "error" found`

0 commit comments

Comments
 (0)