diff options
author | Teddy Wing | 2023-05-18 02:08:01 +0200 |
---|---|---|
committer | Teddy Wing | 2023-05-18 02:08:01 +0200 |
commit | 44ef8673bd55c72f280d34bed6b5eaa795a14c59 (patch) | |
tree | 8674a4df1931a4e9082b93e094f9a924d2fdfa4f | |
parent | 9c66c7865010cec45e7d49019aa344d0774ec7e8 (diff) | |
download | gocapturedrefrace-44ef8673bd55c72f280d34bed6b5eaa795a14c59.tar.bz2 |
Clean up checkShadowing
* Add descriptive comments.
* Remove old in-progress code.
* Change the function name to `findLocalAssignments` which made more
sense given what the function now does.
-rw-r--r-- | gocapturedrefrace.go | 24 |
1 files changed, 5 insertions, 19 deletions
diff --git a/gocapturedrefrace.go b/gocapturedrefrace.go index 3f776e0..fd2cc94 100644 --- a/gocapturedrefrace.go +++ b/gocapturedrefrace.go @@ -20,7 +20,6 @@ package gocapturedrefrace import ( - "fmt" "go/ast" "go/token" "go/types" @@ -85,8 +84,9 @@ func checkClosure(pass *analysis.Pass, funcLit *ast.FuncLit) { // Get the closure's scope. funcScope := pass.TypesInfo.Scopes[funcLit.Type] - localAssignments := checkShadowing(pass, funcLit) - fmt.Printf("localAssignments: %#v\n", localAssignments) + // Build a list of assignments local to funcLit. These will be ignored as + // shadowed variables. + localAssignments := findLocalAssignments(pass, funcLit) ast.Inspect( funcLit, @@ -121,8 +121,6 @@ func checkClosure(pass *analysis.Pass, funcLit *ast.FuncLit) { return true } - // fmt.Printf("variable: %#v\n", variable) - // Ignore captured callable variables, like function arguments. _, isVariableTypeSignature := variable.Type().(*types.Signature) if isVariableTypeSignature { @@ -145,14 +143,11 @@ func checkClosure(pass *analysis.Pass, funcLit *ast.FuncLit) { ) } -// TODO: doc -func checkShadowing( +// findLocalAssignments returns a list of all variable definitions in funcLit. +func findLocalAssignments( pass *analysis.Pass, funcLit *ast.FuncLit, - // funcScope *types.Scope, ) (localAssignments []*ast.Ident) { - // TODO: Plan: Change this function to checkShadowing. Call ast.Inspect and build a list of local assignments in the closure. Then in checkClosure, ignore objects in the local assignments list. - localAssignments = []*ast.Ident{} ast.Inspect( @@ -172,21 +167,12 @@ func checkShadowing( if !ok { return true } - fmt.Printf("assignStmt: %#v\n", ident) if ident == nil { return true } localAssignments = append(localAssignments, ident) - - // 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 true |