diff options
author | Teddy Wing | 2023-05-16 20:15:41 +0200 |
---|---|---|
committer | Teddy Wing | 2023-05-16 20:15:41 +0200 |
commit | 8003b534234a6684d567c932cf9033da7b8bf34b (patch) | |
tree | a2ab84853225647f91222adcc4c20d8e93a85e1d | |
parent | 5afcd02ea68efd3faba578bdcee853c028b14e0e (diff) | |
download | gocapturedrefrace-8003b534234a6684d567c932cf9033da7b8bf34b.tar.bz2 |
Use scope test to report captured variables
Report all variables declared in an outer scope.
This does match variables defined in the closure too. Will need to fix
that.
-rw-r--r-- | gocapturedrefrace.go | 26 | ||||
-rw-r--r-- | testdata/simple.go | 2 |
2 files changed, 15 insertions, 13 deletions
diff --git a/gocapturedrefrace.go b/gocapturedrefrace.go index c49b399..e1f2d41 100644 --- a/gocapturedrefrace.go +++ b/gocapturedrefrace.go @@ -86,8 +86,8 @@ func checkClosure( // TODO: goStmt.Call.Args should also give us something like this. // TODO: Build a list of variables created in the closure - assignments := assignmentsInFunc(pass, funcLit) - fmt.Printf("variable declarations: %#v\n", assignments) + // assignments := assignmentsInFunc(pass, funcLit) + // fmt.Printf("variable declarations: %#v\n", assignments) // TODO: Use ast.GenDecl instead // ast.Scope? @@ -109,11 +109,11 @@ func checkClosure( // TODO: Find out whether ident is a captured reference // Maybe check if variable was not assigned or passed as an argument? - for _, param := range formalParams { - if ident.Obj == param { - return true - } - } + // for _, param := range formalParams { + // if ident.Obj == param { + // return true + // } + // } // TODO: Use (*types.Scope).LookupParent with ident to find out // whether a variable was defined in an outer scope. @@ -128,11 +128,13 @@ func checkClosure( fmt.Printf("In function scope %v\n", scopeObj) } - pass.Reportf( - ident.Pos(), - "variable found %q", - ident, - ) + if funcScope != scope { + pass.Reportf( + ident.Pos(), + "captured reference %s in goroutine closure", + ident, + ) + } return true }, diff --git a/testdata/simple.go b/testdata/simple.go index 8002953..9fdf7a5 100644 --- a/testdata/simple.go +++ b/testdata/simple.go @@ -12,7 +12,7 @@ func main() { capturedReference2 += 1 // want "captured reference capturedReference2 in goroutine closure" copied += 1 - if capturedReference == 1 { + if capturedReference == 1 { // want "captured reference capturedReference in goroutine closure" return } |