aboutsummaryrefslogtreecommitdiffstats
path: root/defererr.go
diff options
context:
space:
mode:
authorTeddy Wing2023-05-20 20:58:49 +0200
committerTeddy Wing2023-05-20 20:58:49 +0200
commit856f21083e814176f9584cdb3d5df4cfdffc4834 (patch)
treec18945e0cab714491e7524e6a50449ace6d94149 /defererr.go
parentf6b3dd27e854ec96e1a29e6034ff1e22c2283719 (diff)
downloadgodefererr-856f21083e814176f9584cdb3d5df4cfdffc4834.tar.bz2
Get error variable names from type loop
Store the index where we find the `error` type in the function signature's return list so we can use it later to get the corresponding variable names, if any.
Diffstat (limited to 'defererr.go')
-rw-r--r--defererr.go15
1 files changed, 8 insertions, 7 deletions
diff --git a/defererr.go b/defererr.go
index c7816e9..338f2c1 100644
--- a/defererr.go
+++ b/defererr.go
@@ -36,12 +36,8 @@ func run(pass *analysis.Pass) (interface{}, error) {
}
funcReturnsError := false
- for _, returnVal := range funcDecl.Type.Results.List {
- fmt.Printf("returnVal: %#v\n", returnVal)
- for _, ident := range returnVal.Names {
- fmt.Printf("returnVal name: %#v\n", ident)
- }
-
+ errorReturnIndex := -1
+ for i, returnVal := range funcDecl.Type.Results.List {
fmt.Printf("returnVal Type: %#v\n", returnVal.Type)
returnIdent, ok := returnVal.Type.(*ast.Ident)
@@ -51,6 +47,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
if returnIdent.Name == "error" {
funcReturnsError = true
+ errorReturnIndex = i
}
}
@@ -58,10 +55,14 @@ func run(pass *analysis.Pass) (interface{}, error) {
// for _, returnVal := range funcType.Results.List {
// }
- if !funcReturnsError {
+ if !funcReturnsError || errorReturnIndex == -1 {
return true
}
+ if len(funcDecl.Type.Results.List[errorReturnIndex].Names) > 0 {
+ fmt.Printf("return error var name: %#v\n", funcDecl.Type.Results.List[errorReturnIndex].Names[0])
+ }
+
ast.Inspect(
funcDecl.Body,
func(node ast.Node) bool {