From 4054b6fdd71811304e4bc7af8c8799becc5338a0 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 17 May 2023 19:51:29 +0200 Subject: Ignore captured variables containing functions --- gocapturedrefrace.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/gocapturedrefrace.go b/gocapturedrefrace.go index e44b397..66be1e9 100644 --- a/gocapturedrefrace.go +++ b/gocapturedrefrace.go @@ -16,7 +16,6 @@ // along with Gocapturedrefrace. If not, see // . - // TODO: package documentation. package gocapturedrefrace @@ -52,8 +51,6 @@ func run(pass *analysis.Pass) (interface{}, error) { // Inspect closure argument list. for _, arg := range funcLit.Type.Params.List { - // TODO: Ignore closures passed as arguments. - // Report reference arguments. _, ok := arg.Type.(*ast.StarExpr) if !ok { @@ -104,15 +101,20 @@ func checkClosure(pass *analysis.Pass, funcLit *ast.FuncLit) { } // Ignore non-variable identifiers. - _, ok = scopeObj.(*types.Var) + variable, ok := scopeObj.(*types.Var) if !ok { return true } + // Ignore captured callable variables, like function arguments. + _, isVariableTypeSignature := variable.Type().(*types.Signature) + // TODO: Ignore shadowing variables. // Identifier was defined in a different scope. - if funcScope != scope { + if funcScope != scope && + !isVariableTypeSignature { + pass.Reportf( ident.Pos(), "captured reference %s in goroutine closure", -- cgit v1.2.3