diff options
author | Teddy Wing | 2023-05-15 03:42:01 +0200 |
---|---|---|
committer | Teddy Wing | 2023-05-15 03:42:01 +0200 |
commit | f433550d70577895f8e18b31b1fe189c195b226a (patch) | |
tree | 5b92472ee0931a7776e52a1ba48a50459a0363a4 | |
parent | 3f4577373527c267df3c89eb4bdaa37480f50cd1 (diff) | |
download | gocapturedrefrace-f433550d70577895f8e18b31b1fe189c195b226a.tar.bz2 |
Get assignments in closure
Turns out this isn't what we want, as assignments means assignments. I
was actually thinking of declarations.
-rw-r--r-- | gocapturedrefrace.go | 34 |
1 files changed, 34 insertions, 0 deletions
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 +} |