aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2023-05-15 03:42:01 +0200
committerTeddy Wing2023-05-15 03:42:01 +0200
commitf433550d70577895f8e18b31b1fe189c195b226a (patch)
tree5b92472ee0931a7776e52a1ba48a50459a0363a4
parent3f4577373527c267df3c89eb4bdaa37480f50cd1 (diff)
downloadgocapturedrefrace-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.go34
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
+}