diff options
| author | Teddy Wing | 2023-05-16 03:51:59 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2023-05-16 03:51:59 +0200 | 
| commit | e4e20e7a4f51e0f0d7ce81285cece8b11c81c542 (patch) | |
| tree | ac1afd9b3bfee056076ba23305a87886bbeef1da | |
| parent | 621747412c3802a8ab09a18b818403fda90adccd (diff) | |
| download | gocapturedrefrace-e4e20e7a4f51e0f0d7ce81285cece8b11c81c542.tar.bz2 | |
Find variable in outer scopes
For every identifier in the closure, check if it exists in an outer
scope.
| -rw-r--r-- | gocapturedrefrace.go | 16 | 
1 files changed, 12 insertions, 4 deletions
| diff --git a/gocapturedrefrace.go b/gocapturedrefrace.go index b4fe9db..f57ad74 100644 --- a/gocapturedrefrace.go +++ b/gocapturedrefrace.go @@ -59,10 +59,10 @@ func run(pass *analysis.Pass) (interface{}, error) {  				// scope := pass.TypesInfo.Scopes[funcLit]  				// scope := pass.TypesInfo.Scopes[goStmt]  				// scope := pass.TypesInfo.Scopes[funcLit.Body] -				scope := pass.TypesInfo.Scopes[funcLit.Type] -				fmt.Println("scope:", scope) +				funcScope := pass.TypesInfo.Scopes[funcLit.Type] +				fmt.Println("scope:", funcScope) -				checkClosure(pass, funcLit) +				checkClosure(pass, funcLit, funcScope)  				return true  			}, @@ -72,7 +72,11 @@ func run(pass *analysis.Pass) (interface{}, error) {  	return nil, nil  } -func checkClosure(pass *analysis.Pass, funcLit *ast.FuncLit) { +func checkClosure( +	pass *analysis.Pass, +	funcLit *ast.FuncLit, +	funcScope *types.Scope, +) {  	formalParams := []*ast.Object{}  	for _, field := range funcLit.Type.Params.List {  		formalParams = append(formalParams, field.Names[0].Obj) @@ -110,6 +114,10 @@ func checkClosure(pass *analysis.Pass, funcLit *ast.FuncLit) {  			// TODO: Use (*types.Scope).LookupParent with ident to find out  			// whether a variable was defined in an outer scope. +			scope, scopeObj := funcScope.LookupParent(ident.Name, ident.NamePos) +			fmt.Println("LookupParent:") +			fmt.Printf("    scope: %#v\n", scope) +			fmt.Printf("    obj  : %#v\n", scopeObj)  			pass.Reportf(  				ident.Pos(), | 
