Skip to content

Commit 801b662

Browse files
committed
review: breaking changes in a BUGFIX version are BAD
1 parent a026bd1 commit 801b662

File tree

7 files changed

+297
-239
lines changed

7 files changed

+297
-239
lines changed

.golangci.reference.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1159,9 +1159,9 @@ linters-settings:
11591159
require-specific: true
11601160

11611161
nonamedreturns:
1162-
# Do not complain about named error, if it is assigned inside defer.
1162+
# Report named error if it is assigned inside defer.
11631163
# Default: false
1164-
allow-error-in-defer: true
1164+
report-error-in-defer: true
11651165

11661166
paralleltest:
11671167
# Ignore missing calls to `t.Parallel()` and only report incorrect uses of it.

pkg/config/linters_settings.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ type NoLintLintSettings struct {
486486
}
487487

488488
type NoNamedReturnsSettings struct {
489-
AllowErrorInDefer bool `mapstructure:"allow-error-in-defer"`
489+
ReportErrorInDefer bool `mapstructure:"report-error-in-defer"`
490490
}
491491
type ParallelTestSettings struct {
492492
IgnoreMissing bool `mapstructure:"ignore-missing"`

pkg/golinters/nonamedreturns.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func NewNoNamedReturns(settings *config.NoNamedReturnsSettings) *goanalysis.Lint
1515
if settings != nil {
1616
cfg = map[string]map[string]interface{}{
1717
a.Name: {
18-
analyzer.FlagAllowErrorInDefer: settings.AllowErrorInDefer,
18+
analyzer.FlagReportErrorInDefer: settings.ReportErrorInDefer,
1919
},
2020
}
2121
}
@@ -25,5 +25,5 @@ func NewNoNamedReturns(settings *config.NoNamedReturnsSettings) *goanalysis.Lint
2525
a.Doc,
2626
[]*analysis.Analyzer{a},
2727
cfg,
28-
).WithLoadMode(goanalysis.LoadModeSyntax)
28+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
2929
}

pkg/lint/lintersdb/manager.go

+1
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
627627

628628
linter.NewConfig(golinters.NewNoNamedReturns(noNamedReturnsCfg)).
629629
WithSince("v1.46.0").
630+
WithLoadForGoAnalysis().
630631
WithPresets(linter.PresetStyle).
631632
WithURL("https://github.com/firefart/nonamedreturns"),
632633

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
linters-settings:
22
nonamedreturns:
3-
allow-error-in-defer: true
3+
report-error-in-defer: true

test/testdata/nonamedreturns.go

+221-64
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,281 @@
11
// args: -Enonamedreturns
22
package testdata
33

4-
import "fmt"
4+
import "errors"
55

6-
type asdf struct {
7-
test string
6+
func simple() (err error) {
7+
defer func() {
8+
err = nil
9+
}()
10+
return
811
}
912

10-
func noParams() {
13+
func twoReturnParams() (i int, err error) { // ERROR `named return "i" with type "int" found`
14+
defer func() {
15+
i = 0
16+
err = nil
17+
}()
1118
return
1219
}
1320

14-
var c = func() {
21+
func allUnderscoresExceptError() (_ int, err error) {
22+
defer func() {
23+
err = nil
24+
}()
1525
return
1626
}
1727

18-
var d = func() error {
19-
return nil
28+
func customName() (myName error) {
29+
defer func() {
30+
myName = nil
31+
}()
32+
return
2033
}
2134

22-
var e = func() (err error) { // ERROR `named return "err" with type "error" found`
23-
err = nil
35+
func errorIsNoAssigned() (err error) { // ERROR `named return "err" with type "error" found`
36+
defer func() {
37+
_ = err
38+
processError(err)
39+
if err == nil {
40+
}
41+
switch err {
42+
case nil:
43+
default:
44+
}
45+
}()
2446
return
2547
}
2648

27-
var e2 = func() (_ error) {
49+
func shadowVariable() (err error) { // ERROR `named return "err" with type "error" found`
50+
defer func() {
51+
err := errors.New("xxx")
52+
_ = err
53+
}()
2854
return
2955
}
3056

31-
func deferWithError() (err error) { // ERROR `named return "err" with type "error" found`
57+
func shadowVariableButAssign() (err error) {
3258
defer func() {
33-
err = nil // use flag to allow this
59+
{
60+
err := errors.New("xxx")
61+
_ = err
62+
}
63+
err = nil
3464
}()
3565
return
3666
}
3767

38-
var (
39-
f = func() {
40-
return
41-
}
68+
func shadowVariable2() (err error) { // ERROR `named return "err" with type "error" found`
69+
defer func() {
70+
a, err := doSomething()
71+
_ = a
72+
_ = err
73+
}()
74+
return
75+
}
4276

43-
g = func() error {
44-
return nil
45-
}
77+
type errorAlias = error
4678

47-
h = func() (err error) { // ERROR `named return "err" with type "error" found`
79+
func errorAliasIsTheSame() (err errorAlias) {
80+
defer func() {
4881
err = nil
49-
return
50-
}
82+
}()
83+
return
84+
}
85+
86+
type myError error // linter doesn't check underlying type (yet?)
87+
88+
func customTypeWithErrorUnderline() (err myError) { // ERROR `named return "err" with type "myError" found`
89+
defer func() {
90+
err = nil
91+
}()
92+
return
93+
}
94+
95+
type myError2 interface{ error } // linter doesn't check interfaces
96+
97+
func customTypeWithTheSameInterface() (err myError2) { // ERROR `named return "err" with type "myError2" found`
98+
defer func() {
99+
err = nil
100+
}()
101+
return
102+
}
103+
104+
var _ error = myError3{}
105+
106+
type myError3 struct{} // linter doesn't check interfaces
107+
108+
func (m myError3) Error() string { return "" }
109+
110+
func customTypeImplementingErrorInterface() (err myError3) { // ERROR `named return "err" with type "myError3" found`
111+
defer func() {
112+
err = struct{}{}
113+
}()
114+
return
115+
}
51116

52-
h2 = func() (_ error) {
117+
func shadowErrorType() {
118+
type error interface { // linter understands that this is not built-in error, even if it has the same name
119+
Error() string
120+
}
121+
do := func() (err error) { // ERROR `named return "err" with type "error" found`
122+
defer func() {
123+
err = nil
124+
}()
53125
return
54126
}
55-
)
127+
do()
128+
}
56129

57-
// this should not match as the implementation does not need named parameters (see below)
58-
type funcDefintion func(arg1, arg2 interface{}) (num int, err error)
130+
func notTheLast() (err error, _ int) {
131+
defer func() {
132+
err = nil
133+
}()
134+
return
135+
}
59136

60-
func funcDefintionImpl(arg1, arg2 interface{}) (int, error) {
61-
return 0, nil
137+
func twoErrorsCombined() (err1, err2 error) {
138+
defer func() {
139+
err1 = nil
140+
err2 = nil
141+
}()
142+
return
62143
}
63144

64-
func funcDefintionImpl2(arg1, arg2 interface{}) (num int, err error) { // ERROR `named return "num" with type "int" found`
65-
return 0, nil
145+
func twoErrorsSeparated() (err1 error, err2 error) {
146+
defer func() {
147+
err1 = nil
148+
err2 = nil
149+
}()
150+
return
66151
}
67152

68-
func funcDefintionImpl3(arg1, arg2 interface{}) (num int, _ error) { // ERROR `named return "num" with type "int" found`
69-
return 0, nil
153+
func errorSlice() (err []error) { // ERROR `named return "err" with type "\[\]error" found`
154+
defer func() {
155+
err = nil
156+
}()
157+
return
70158
}
71159

72-
func funcDefintionImpl4(arg1, arg2 interface{}) (_ int, _ error) {
73-
return 0, nil
160+
func deferWithVariable() (err error) { // ERROR `named return "err" with type "error" found`
161+
f := func() {
162+
err = nil
163+
}
164+
defer f() // linter can't catch closure passed via variable (yet?)
165+
return
74166
}
75167

76-
var funcVar = func() (msg string) { // ERROR `named return "msg" with type "string" found`
77-
msg = "c"
78-
return msg
168+
func uberMultierr() (err error) { // ERROR `named return "err" with type "error" found`
169+
defer func() {
170+
multierrAppendInto(&err, nil) // linter doesn't allow it (yet?)
171+
}()
172+
return
79173
}
80174

81-
var funcVar2 = func() (_ string) {
82-
msg := "c"
83-
return msg
175+
func deferInDefer() (err error) {
176+
defer func() {
177+
defer func() {
178+
err = nil
179+
}()
180+
}()
181+
return
84182
}
85183

86-
func test() {
87-
a := funcVar()
88-
_ = a
184+
func twoDefers() (err error) {
185+
defer func() {}()
186+
defer func() {
187+
err = nil
188+
}()
189+
return
190+
}
89191

90-
var function funcDefintion
91-
function = funcDefintionImpl
92-
i, err := function("", "")
93-
_ = i
94-
_ = err
95-
function = funcDefintionImpl2
96-
i, err = function("", "")
97-
_ = i
98-
_ = err
192+
func callFunction() (err error) {
193+
defer func() {
194+
_, err = doSomething()
195+
}()
196+
return
99197
}
100198

101-
func good(i string) string {
102-
return i
199+
func callFunction2() (err error) {
200+
defer func() {
201+
var a int
202+
a, err = doSomething()
203+
_ = a
204+
}()
205+
return
103206
}
104207

105-
func bad(i string, a, b int) (ret1 string, ret2 interface{}, ret3, ret4 int, ret5 asdf) { // ERROR `named return "ret1" with type "string" found`
106-
x := "dummy"
107-
return fmt.Sprintf("%s", x), nil, 1, 2, asdf{}
208+
func deepInside() (err error) {
209+
if true {
210+
switch true {
211+
case false:
212+
for i := 0; i < 10; i++ {
213+
go func() {
214+
select {
215+
default:
216+
defer func() {
217+
if true {
218+
switch true {
219+
case false:
220+
for j := 0; j < 10; j++ {
221+
go func() {
222+
select {
223+
default:
224+
err = nil
225+
}
226+
}()
227+
}
228+
}
229+
}
230+
}()
231+
}
232+
}()
233+
}
234+
}
235+
}
236+
return
108237
}
109238

110-
func bad2() (msg string, err error) { // ERROR `named return "msg" with type "string" found`
111-
msg = ""
112-
err = nil
239+
var goodFuncLiteral = func() (err error) {
240+
defer func() {
241+
err = nil
242+
}()
113243
return
114244
}
115245

116-
func myLog(format string, args ...interface{}) {
246+
var badFuncLiteral = func() (err error) { // ERROR `named return "err" with type "error" found`
247+
defer func() {
248+
_ = err
249+
}()
117250
return
118251
}
119252

120-
type obj struct{}
253+
func funcLiteralInsideFunc() error {
254+
do := func() (err error) {
255+
defer func() {
256+
err = nil
257+
}()
258+
return
259+
}
260+
return do()
261+
}
121262

122-
func (o *obj) func1() (err error) { return nil } // ERROR `named return "err" with type "error" found`
263+
type x struct{}
264+
265+
func (x) goodMethod() (err error) {
266+
defer func() {
267+
err = nil
268+
}()
269+
return
270+
}
271+
272+
func (x) badMethod() (err error) { // ERROR `named return "err" with type "error" found`
273+
defer func() {
274+
_ = err
275+
}()
276+
return
277+
}
123278

124-
func (o *obj) func2() (_ error) { return nil }
279+
func processError(error) {}
280+
func doSomething() (int, error) { return 10, nil }
281+
func multierrAppendInto(*error, error) bool { return false } // https://pkg.go.dev/go.uber.org/multierr#AppendInto

0 commit comments

Comments
 (0)