From ace55a2a1696e3b60891b5d731705ead72446f7d Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 18 May 2023 01:46:16 +0200 Subject: Try to ignore shadowed assignments Not working yet. Does print the right thing, but since we're also matching `*ast.Ident`s, we must be getting the shadowed declarations as Idents and including them in the reported diagnostics. --- gocapturedrefrace.go | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/gocapturedrefrace.go b/gocapturedrefrace.go index d5eaac7..369b107 100644 --- a/gocapturedrefrace.go +++ b/gocapturedrefrace.go @@ -87,7 +87,9 @@ func checkClosure(pass *analysis.Pass, funcLit *ast.FuncLit) { ast.Inspect( funcLit, func(node ast.Node) bool { - checkShadowing(pass, node, funcScope) + if isShadowingDeclaration(pass, node, funcScope) { + return true + } ident, ok := node.(*ast.Ident) if !ok { @@ -137,37 +139,41 @@ func checkClosure(pass *analysis.Pass, funcLit *ast.FuncLit) { } // TODO: doc -func checkShadowing( +func isShadowingDeclaration( pass *analysis.Pass, node ast.Node, funcScope *types.Scope, -) { +) bool { assignStmt, ok := node.(*ast.AssignStmt) if !ok { - return + return false } if assignStmt.Tok != token.DEFINE { - return + return false } for _, lhs := range assignStmt.Lhs { ident, ok := lhs.(*ast.Ident) if !ok { - return + return false } fmt.Printf("assignStmt: %#v\n", ident) - if ident != nil { - return + if ident == nil { + return false } + return true + // TODO: If ident is in parent, ignore it an move on. // scope, scopeObj := funcScope.LookupParent(ident.Name, ident.NamePos) - // + // // // Identifier is local to the closure. // if scope == nil && scopeObj == nil { // return // } } + + return false } -- cgit v1.2.3