refactor: nested context recursion

This commit is contained in:
Gabriel Augendre 2025-01-13 23:08:04 +01:00
parent ef9d47d1f0
commit f887074f5d

View file

@ -156,44 +156,29 @@ func getBody(node ast.Node) (*ast.BlockStmt, error) {
func findNestedContext(pass *analysis.Pass, node ast.Node, 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 { switch typedStmt := stmt.(type) {
found := findNestedContext(pass, node, inner.List) case *ast.BlockStmt:
if found != nil { if found := findNestedContext(pass, node, typedStmt.List); found != nil {
return found return found
} }
} case *ast.IfStmt:
if found := findNestedContext(pass, node, typedStmt.Body.List); found != nil {
if inner, ok := stmt.(*ast.IfStmt); ok {
found := findNestedContext(pass, node, inner.Body.List)
if found != nil {
return found return found
} }
} case *ast.SwitchStmt:
if found := findNestedContext(pass, node, typedStmt.Body.List); found != nil {
if inner, ok := stmt.(*ast.SwitchStmt); ok {
found := findNestedContext(pass, node, inner.Body.List)
if found != nil {
return found return found
} }
} case *ast.CaseClause:
if found := findNestedContext(pass, node, typedStmt.Body); found != nil {
if inner, ok := stmt.(*ast.CaseClause); ok {
found := findNestedContext(pass, node, inner.Body)
if found != nil {
return found return found
} }
} case *ast.SelectStmt:
if found := findNestedContext(pass, node, typedStmt.Body.List); found != nil {
if inner, ok := stmt.(*ast.SelectStmt); ok {
found := findNestedContext(pass, node, inner.Body.List)
if found != nil {
return found return found
} }
} case *ast.CommClause:
if found := findNestedContext(pass, node, typedStmt.Body); found != nil {
if inner, ok := stmt.(*ast.CommClause); ok {
found := findNestedContext(pass, node, inner.Body)
if found != nil {
return found return found
} }
} }