diff options
| -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  	}()  } | 
