Skip to content

Commit f79863a

Browse files
committed
ignore underscore names
Name `_` is the same as no name and should be ignored.
1 parent 853036d commit f79863a

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

analyzer/analyzer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ func run(pass *analysis.Pass) (interface{}, error) {
6666
}
6767

6868
for _, n := range p.Names {
69+
if n.Name == "_" {
70+
continue
71+
}
72+
6973
if allowErrorInDefer {
7074
if ident, ok := p.Type.(*ast.Ident); ok {
7175
if ident.Name == "error" && findDeferWithErrorAssignment(funcBody, n.Name) {

testdata/src/allow-error-in-defer/allow_error_in_defer.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@ func twoReturnParams() (i int, err error) { // want `named return "i" with type
1515
return
1616
}
1717

18-
// TODO: enable test after https://github.com/firefart/nonamedreturns/pull/7
19-
//func allUnderscoresExceptError() (_ int, err error) {
20-
// defer func() {
21-
// err = nil
22-
// }()
23-
// return
24-
//}
18+
func allUnderscoresExceptError() (_ int, err error) {
19+
defer func() {
20+
err = nil
21+
}()
22+
return
23+
}
2524

2625
func customName() (myName error) {
2726
defer func() {
@@ -70,10 +69,8 @@ func customType() (err myError) { // want `named return "err" with type "myError
7069
return
7170
}
7271

73-
// TODO: replace `i` with `_` after https://github.com/firefart/nonamedreturns/pull/7
74-
func notTheLast() (err error, i int) { // want `named return "i" with type "int" found`
72+
func notTheLast() (err error, _ int) {
7573
defer func() {
76-
i = 0
7774
err = nil
7875
}()
7976
return

testdata/src/default-config/default_config.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ var e = func() (err error) { // want `named return "err" with type "error" found
2323
return
2424
}
2525

26+
var e2 = func() (_ error) {
27+
return
28+
}
29+
2630
func deferWithError() (err error) { // want `named return "err" with type "error" found`
2731
defer func() {
2832
err = nil // use flag to allow this
@@ -43,6 +47,10 @@ var (
4347
err = nil
4448
return
4549
}
50+
51+
h2 = func() (_ error) {
52+
return
53+
}
4654
)
4755

4856
// this should not match as the implementation does not need named parameters (see below)
@@ -56,11 +64,24 @@ func funcDefintionImpl2(arg1, arg2 interface{}) (num int, err error) { // want `
5664
return 0, nil
5765
}
5866

67+
func funcDefintionImpl3(arg1, arg2 interface{}) (num int, _ error) { // want `named return "num" with type "int" found`
68+
return 0, nil
69+
}
70+
71+
func funcDefintionImpl4(arg1, arg2 interface{}) (_ int, _ error) {
72+
return 0, nil
73+
}
74+
5975
var funcVar = func() (msg string) { // want `named return "msg" with type "string" found`
6076
msg = "c"
6177
return msg
6278
}
6379

80+
var funcVar2 = func() (_ string) {
81+
msg := "c"
82+
return msg
83+
}
84+
6485
func test() {
6586
a := funcVar()
6687
_ = a
@@ -98,3 +119,5 @@ func myLog(format string, args ...interface{}) {
98119
type obj struct{}
99120

100121
func (o *obj) func1() (err error) { return nil } // want `named return "err" with type "error" found`
122+
123+
func (o *obj) func2() (_ error) { return nil }

0 commit comments

Comments
 (0)