From 30fed3b90b79c6aa1221f933e82b52dc5b07b5a6 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Mon, 15 May 2023 03:05:27 +0200 Subject: Ignore variables from the closure's formal arguments Don't report variables that were passed as arguments to the closure. --- gocapturedrefrace.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/gocapturedrefrace.go b/gocapturedrefrace.go index 9544d48..454156f 100644 --- a/gocapturedrefrace.go +++ b/gocapturedrefrace.go @@ -51,11 +51,11 @@ func run(pass *analysis.Pass) (interface{}, error) { } func checkClosure(pass *analysis.Pass, funcLit *ast.FuncLit) { - fmt.Print("Params: ") + formalParams := []*ast.Object{} for _, field := range funcLit.Type.Params.List { - fmt.Printf("%#v, ", field.Names[0].Name) + formalParams = append(formalParams, field.Names[0].Obj) } - fmt.Println() + fmt.Printf("%#v\n", formalParams) ast.Inspect( funcLit, @@ -72,6 +72,12 @@ func checkClosure(pass *analysis.Pass, funcLit *ast.FuncLit) { // 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 + } + } + pass.Reportf( ident.Pos(), "variable found %q", -- cgit v1.2.3