use node instead of block

This commit is contained in:
Venkatesh Kotwade 2024-08-26 01:28:11 +05:30
parent 07aa8cc6a2
commit 71bde6a5f6

View file

@ -38,7 +38,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
return return
} }
assignStmt := findNestedContext(pass, body, body.List) assignStmt := findNestedContext(pass, node, body.List)
if assignStmt == nil { if assignStmt == nil {
return return
} }
@ -108,7 +108,7 @@ func getBody(node ast.Node) (*ast.BlockStmt, error) {
return nil, errUnknown return nil, errUnknown
} }
func findNestedContext(pass *analysis.Pass, block *ast.BlockStmt, stmts []ast.Stmt) *ast.AssignStmt { func findNestedContext(pass *analysis.Pass, node ast.Node, stmts []ast.Stmt) *ast.AssignStmt {
for _, stmt := range stmts { for _, stmt := range stmts {
// Recurse if necessary // Recurse if necessary
if inner, ok := stmt.(*ast.BlockStmt); ok { if inner, ok := stmt.(*ast.BlockStmt); ok {
@ -119,35 +119,35 @@ func findNestedContext(pass *analysis.Pass, block *ast.BlockStmt, stmts []ast.St
} }
if inner, ok := stmt.(*ast.IfStmt); ok { if inner, ok := stmt.(*ast.IfStmt); ok {
found := findNestedContext(pass, inner.Body, inner.Body.List) found := findNestedContext(pass, inner, inner.Body.List)
if found != nil { if found != nil {
return found return found
} }
} }
if inner, ok := stmt.(*ast.SwitchStmt); ok { if inner, ok := stmt.(*ast.SwitchStmt); ok {
found := findNestedContext(pass, inner.Body, inner.Body.List) found := findNestedContext(pass, inner, inner.Body.List)
if found != nil { if found != nil {
return found return found
} }
} }
if inner, ok := stmt.(*ast.CaseClause); ok { if inner, ok := stmt.(*ast.CaseClause); ok {
found := findNestedContext(pass, block, inner.Body) found := findNestedContext(pass, node, inner.Body)
if found != nil { if found != nil {
return found return found
} }
} }
if inner, ok := stmt.(*ast.SelectStmt); ok { if inner, ok := stmt.(*ast.SelectStmt); ok {
found := findNestedContext(pass, inner.Body, inner.Body.List) found := findNestedContext(pass, inner, inner.Body.List)
if found != nil { if found != nil {
return found return found
} }
} }
if inner, ok := stmt.(*ast.CommClause); ok { if inner, ok := stmt.(*ast.CommClause); ok {
found := findNestedContext(pass, block, inner.Body) found := findNestedContext(pass, node, inner.Body)
if found != nil { if found != nil {
return found return found
} }
@ -175,7 +175,7 @@ func findNestedContext(pass *analysis.Pass, block *ast.BlockStmt, stmts []ast.St
// allow assignment to non-pointer children of values defined within the loop // allow assignment to non-pointer children of values defined within the loop
if lhs := getRootIdent(pass, assignStmt.Lhs[0]); lhs != nil { if lhs := getRootIdent(pass, assignStmt.Lhs[0]); lhs != nil {
if obj := pass.TypesInfo.ObjectOf(lhs); obj != nil { if obj := pass.TypesInfo.ObjectOf(lhs); obj != nil {
if checkObjectScopeWithinBlock(obj.Parent(), block) { if checkObjectScopeWithinNode(obj.Parent(), node) {
continue // definition is within the loop continue // definition is within the loop
} }
} }
@ -187,12 +187,12 @@ func findNestedContext(pass *analysis.Pass, block *ast.BlockStmt, stmts []ast.St
return nil return nil
} }
func checkObjectScopeWithinBlock(scope *types.Scope, block *ast.BlockStmt) bool { func checkObjectScopeWithinNode(scope *types.Scope, node ast.Node) bool {
if scope == nil { if scope == nil {
return false return false
} }
if scope.Pos() >= block.Pos() && scope.End() <= block.End() { if scope.Pos() >= node.Pos() && scope.End() <= node.End() {
return true return true
} }