diff options
| author | Teddy Wing | 2023-05-15 02:27:38 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2023-05-15 02:27:38 +0200 | 
| commit | 47f01bb1f6e75a071c6ee9c7e070b0c03634be40 (patch) | |
| tree | d61246fd7d78c3418a9223b3c1fe4626d59458c8 | |
| parent | 6d91d473f24d6825170e494852b26ffc748fe542 (diff) | |
| download | gocapturedrefrace-47f01bb1f6e75a071c6ee9c7e070b0c03634be40.tar.bz2 | |
Find variables in `go func` closures
Inspect function literals run by `go` statements, and find all variables
in those function literals.
| -rw-r--r-- | gocapturedrefrace.go | 36 | ||||
| -rw-r--r-- | testdata/simple.go | 2 | 
2 files changed, 33 insertions, 5 deletions
| diff --git a/gocapturedrefrace.go b/gocapturedrefrace.go index 762473b..21a0d16 100644 --- a/gocapturedrefrace.go +++ b/gocapturedrefrace.go @@ -2,6 +2,7 @@ package gocapturedrefrace  import (  	"bytes" +	"fmt"  	"go/ast"  	"go/printer" @@ -30,11 +31,16 @@ func run(pass *analysis.Pass) (interface{}, error) {  					panic(err)  				} -				pass.Reportf( -					goStmt.Pos(), -					"go statement found %q", -					printedNode, -				) +				fmt.Printf("%#v\n", goStmt) + +				// TODO: Get func literal of go statement +				// TODO: Get variables in func literal +				funcLit, ok := goStmt.Call.Fun.(*ast.FuncLit) +				if !ok { +					return true +				} + +				checkClosure(pass, funcLit)  				return true  			}, @@ -43,3 +49,23 @@ func run(pass *analysis.Pass) (interface{}, error) {  	return nil, nil  } + +func checkClosure(pass *analysis.Pass, funcLit *ast.FuncLit) { +	ast.Inspect( +		funcLit, +		func(node ast.Node) bool { +			variable, ok := node.(*ast.Ident) +			if !ok { +				return true +			} + +			pass.Reportf( +				variable.Pos(), +				"variable found %q", +				variable, +			) + +			return true +		}, +	) +} diff --git a/testdata/simple.go b/testdata/simple.go index 496be52..4b4c23a 100644 --- a/testdata/simple.go +++ b/testdata/simple.go @@ -2,8 +2,10 @@ package main  func main() {  	capturedReference := 0 +	capturedReference2 := 1  	go func() {  		capturedReference += 1 +		capturedReference2 += 1  	}()  } | 
