Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit23f3439

Browse files
committed
correct error line on multiple value return
1 parent4a4d343 commit23f3439

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

‎nilerr.go‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ func run(pass *analysis.Pass) (interface{}, error) {
2828
cmaps:=pass.ResultOf[commentmap.Analyzer].(comment.Maps)
2929

3030
reportFail:=func(v ssa.Value,ret*ssa.Return,formatstring) {
31-
errPos:=v.Pos()
32-
errLine:=pass.Fset.File(errPos).Line(errPos)
3331
pos:=ret.Pos()
34-
line:=pass.Fset.File(pos).Line(pos)
32+
line:=getNodeLineNumber(pass,ret)
33+
errLine:=getValueLineNumber(pass,v)
3534
if!cmaps.IgnoreLine(pass.Fset,line,"nilerr") {
3635
pass.Reportf(pos,format,errLine)
3736
}
@@ -57,6 +56,21 @@ func run(pass *analysis.Pass) (interface{}, error) {
5756
returnnil,nil
5857
}
5958

59+
funcgetValueLineNumber(pass*analysis.Pass,v ssa.Value)int {
60+
value:=v
61+
ifextract,ok:=value.(*ssa.Extract);ok {
62+
value=extract.Tuple
63+
}
64+
65+
pos:=value.Pos()
66+
returnpass.Fset.File(pos).Line(pos)
67+
}
68+
69+
funcgetNodeLineNumber(pass*analysis.Pass,node ssa.Node)int {
70+
pos:=node.Pos()
71+
returnpass.Fset.File(pos).Line(pos)
72+
}
73+
6074
varerrType=types.Universe.Lookup("error").Type().Underlying().(*types.Interface)
6175

6276
funcbinOpErrNil(b*ssa.BasicBlock,op token.Token) ssa.Value {

‎testdata/src/a/a.go‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,14 @@ import (
77
"testing"
88
)
99

10-
funcdo()error {
11-
returnnil
12-
}
13-
1410
funcf()error {
1511
err:=do()
1612
iferr!=nil {
17-
returnnil// want "error is not nil \\(line15\\) but it returns nil"
13+
returnnil// want "error is not nil \\(line11\\) but it returns nil"
1814
}
1915

2016
iferr:=do();err!=nil {
21-
returnnil// want "error is not nil \\(line20\\) but it returns nil"
17+
returnnil// want "error is not nil \\(line16\\) but it returns nil"
2218
}
2319

2420
iferr:=do();err!=nil {
@@ -32,10 +28,16 @@ func f() error {
3228
funcg()error {
3329
err:=do()
3430
iferr==nil {
35-
returnerr// want "error is nil \\(line33\\) but it returns error"
31+
returnerr// want "error is nil \\(line29\\) but it returns error"
3632
}
3733

3834
iferr:=do();err==nil {
35+
returnerr// want "error is nil \\(line 34\\) but it returns error"
36+
}
37+
38+
bytes,err:=do2()
39+
iferr==nil {
40+
_=bytes
3941
returnerr// want "error is nil \\(line 38\\) but it returns error"
4042
}
4143

@@ -87,7 +89,7 @@ func h() {
8789
break
8890
}
8991
}
90-
returnnil// want "error is not nil \\(line86\\) but it returns nil"
92+
returnnil// want "error is not nil \\(line88\\) but it returns nil"
9193
}
9294
_=f0
9395

@@ -102,6 +104,14 @@ func h() {
102104
_=f1
103105
}
104106

107+
funcdo()error {
108+
returnnil
109+
}
110+
111+
funcdo2() ([]byte,error) {
112+
returnnil,nil
113+
}
114+
105115
funcCustomLoggingFunc(errerror) {
106116
log.Printf("%+v",err)
107117
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp