From c76dd021f7a3df0f524b8c9e8e710950a0b31fb6 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 18 May 2023 03:40:44 +0200 Subject: varDeclaration: Return all identifiers in declaration Declarations can include multiple identifiers. Return all of these from the function and rename it accordingly. --- gocapturedrefrace.go | 20 +++++++++++--------- testdata/shadow.go | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/gocapturedrefrace.go b/gocapturedrefrace.go index cab0b4f..5be3656 100644 --- a/gocapturedrefrace.go +++ b/gocapturedrefrace.go @@ -180,9 +180,9 @@ func findLocalVarDeclarations( } case *ast.GenDecl: - decl := varDeclaration(node) - if decl != nil { - declarations = append(declarations, decl) + decls := varDeclarations(node) + if decls != nil { + declarations = append(declarations, decls...) } } @@ -193,23 +193,25 @@ func findLocalVarDeclarations( return declarations } -// varDeclaration returns the identifier corresponding to variable declarations -// in decl, or nil if decl is not a variable declaration. -func varDeclaration(decl *ast.GenDecl) *ast.Ident { +// varDeclarations returns the identifiers corresponding to variable +// declarations in decl, or nil if decl is not a variable declaration. +func varDeclarations(decl *ast.GenDecl) (declarations []*ast.Ident) { if decl.Tok != token.VAR { return nil } + declarations = []*ast.Ident{} + for _, spec := range decl.Specs { valueSpec, ok := spec.(*ast.ValueSpec) if !ok { - return nil + return declarations } for _, ident := range valueSpec.Names { - return ident + declarations = append(declarations, ident) } } - return nil + return declarations } diff --git a/testdata/shadow.go b/testdata/shadow.go index 023df57..a8c2b5f 100644 --- a/testdata/shadow.go +++ b/testdata/shadow.go @@ -31,3 +31,23 @@ func shadow() { } }() } + +func multiIdentifierDeclaration() { + var err1, err2 error + err1 = nil + err2 = nil + if err1 != nil || err2 != nil { + log.Print(err1, err2) + } + + go func() { + // err1 and err2 are redeclared here and shadow the outer scope. No + // diagnostic should be printed. + var err1, err2 error + err1 = errors.New("shadowing declaration err1") + err2 = errors.New("shadowing declaration err2") + if err1 != nil || err2 != nil { + log.Print(err1, err2) + } + }() +} -- cgit v1.2.3