diff options
| author | Teddy Wing | 2023-05-19 01:28:04 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2023-05-19 01:28:04 +0200 | 
| commit | 0224fc93a90adcd061679380fa85bef6311a259b (patch) | |
| tree | 289c4fd04845fd7a3c9de13d2a02d24d96d3e3c9 | |
| parent | 1d6aca277d7a2a617810daa74fc0a01d6bd5f776 (diff) | |
| download | godefererr-0224fc93a90adcd061679380fa85bef6311a259b.tar.bz2 | |
Work out how to get type name of assignments in `defer`
| -rw-r--r-- | defererr.go | 38 | 
1 files changed, 37 insertions, 1 deletions
| diff --git a/defererr.go b/defererr.go index 6bd1188..31529b1 100644 --- a/defererr.go +++ b/defererr.go @@ -5,6 +5,7 @@ import (  	"fmt"  	"go/ast"  	"go/token" +	"go/types"  	"golang.org/x/tools/go/analysis"  ) @@ -87,7 +88,42 @@ func run(pass *analysis.Pass) (interface{}, error) {  								}  								fmt.Printf("assignStmt: %#v\n", assignStmt) -								fmt.Printf("token: %d\n", token.DEFINE) + +								// TODO: Get type of Lhs, check if "error" +								// If "error", then ensure error return is declared in signature + +								for _, variable := range assignStmt.Lhs { +									ident, ok := variable.(*ast.Ident) +									if !ok { +										continue +									} + +									obj := pass.TypesInfo.Defs[ident] + +									valueSpec, ok := ident.Obj.Decl.(*ast.ValueSpec) +									if !ok { +										continue +									} + +									fmt.Printf("variable: %#v\n", ident) +									fmt.Printf("variable.obj: %#v\n", ident.Obj) +									fmt.Printf("variable.obj.type: %#v\n", ident.Obj.Type) +									fmt.Printf("variable.obj.valuespec: %#v\n", valueSpec) +									fmt.Printf("variable.obj.valuespec.type: %#v\n", valueSpec.Type) +									fmt.Printf("obj: %#v\n", obj) + +									t := pass.TypesInfo.Types[variable] +									fmt.Printf("type: %#v\n", t) +									fmt.Printf("type.type: %#v\n", t.Type) + +									named, ok := t.Type.(*types.Named) +									if !ok { +										continue +									} + +									fmt.Printf("type.type.obj: %#v\n", named.Obj()) +									fmt.Printf("type.type.obj: %#v\n", named.Obj().Name()) +								}  								return true  							}, | 
