From f433550d70577895f8e18b31b1fe189c195b226a Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Mon, 15 May 2023 03:42:01 +0200 Subject: Get assignments in closure Turns out this isn't what we want, as assignments means assignments. I was actually thinking of declarations. --- gocapturedrefrace.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/gocapturedrefrace.go b/gocapturedrefrace.go index 11f2ac1..3ac8022 100644 --- a/gocapturedrefrace.go +++ b/gocapturedrefrace.go @@ -58,6 +58,8 @@ func checkClosure(pass *analysis.Pass, funcLit *ast.FuncLit) { fmt.Printf("%#v\n", formalParams) // TODO: Build a list of variables created in the closure + assignments := assignmentsInFunc(pass, funcLit) + fmt.Printf("%#v\n", assignments) ast.Inspect( funcLit, @@ -90,3 +92,35 @@ func checkClosure(pass *analysis.Pass, funcLit *ast.FuncLit) { }, ) } + +func assignmentsInFunc( + pass *analysis.Pass, + funcLit *ast.FuncLit, +) []string { + assignments := []string{} + + ast.Inspect( + funcLit, + func(node ast.Node) bool { + ident, ok := node.(*ast.Ident) + if !ok { + return true + } + + if ident.Obj == nil || ident.Obj.Decl == nil { + return true + } + + _, ok = ident.Obj.Decl.(*ast.AssignStmt) + if !ok { + return true + } + + assignments = append(assignments, ident.Name) + + return true + }, + ) + + return assignments +} -- cgit v1.2.3