diff options
author | Teddy Wing | 2023-05-18 01:38:53 +0200 |
---|---|---|
committer | Teddy Wing | 2023-05-18 01:38:53 +0200 |
commit | 0fddc640af772014e5a87db394657f03e273c528 (patch) | |
tree | d787d143b9853ca77b499eb6fd21822228a79b4c | |
parent | 939b3aad88ce3043906d95329fecd8302ea3d9d7 (diff) | |
download | gocapturedrefrace-0fddc640af772014e5a87db394657f03e273c528.tar.bz2 |
Find shadowed variable assignments
We don't want to report shadowed variables.
-rw-r--r-- | gocapturedrefrace.go | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/gocapturedrefrace.go b/gocapturedrefrace.go index ff5ac9a..d5eaac7 100644 --- a/gocapturedrefrace.go +++ b/gocapturedrefrace.go @@ -20,7 +20,9 @@ package gocapturedrefrace import ( + "fmt" "go/ast" + "go/token" "go/types" "golang.org/x/tools/go/analysis" @@ -85,6 +87,8 @@ func checkClosure(pass *analysis.Pass, funcLit *ast.FuncLit) { ast.Inspect( funcLit, func(node ast.Node) bool { + checkShadowing(pass, node, funcScope) + ident, ok := node.(*ast.Ident) if !ok { return true @@ -108,6 +112,8 @@ 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 { @@ -129,3 +135,39 @@ func checkClosure(pass *analysis.Pass, funcLit *ast.FuncLit) { }, ) } + +// TODO: doc +func checkShadowing( + pass *analysis.Pass, + node ast.Node, + funcScope *types.Scope, +) { + assignStmt, ok := node.(*ast.AssignStmt) + if !ok { + return + } + + if assignStmt.Tok != token.DEFINE { + return + } + + for _, lhs := range assignStmt.Lhs { + ident, ok := lhs.(*ast.Ident) + if !ok { + return + } + fmt.Printf("assignStmt: %#v\n", ident) + + if ident != nil { + return + } + + // 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 + // } + } +} |