feat: also added support for multiple contexts

This commit is contained in:
Venkatesh Kotwade 2024-08-26 01:41:16 +05:30
parent 387c533fad
commit c2c0e62d59
2 changed files with 13 additions and 5 deletions

View file

@ -110,21 +110,21 @@ func findNestedContext(pass *analysis.Pass, node ast.Node, stmts []ast.Stmt) *as
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 {
found := findNestedContext(pass, inner, inner.List) found := findNestedContext(pass, node, inner.List)
if found != nil { if found != nil {
return found return found
} }
} }
if inner, ok := stmt.(*ast.IfStmt); ok { if inner, ok := stmt.(*ast.IfStmt); ok {
found := findNestedContext(pass, inner, inner.Body.List) found := findNestedContext(pass, node, 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, inner.Body.List) found := findNestedContext(pass, node, inner.Body.List)
if found != nil { if found != nil {
return found return found
} }
@ -138,7 +138,7 @@ func findNestedContext(pass *analysis.Pass, node ast.Node, stmts []ast.Stmt) *as
} }
if inner, ok := stmt.(*ast.SelectStmt); ok { if inner, ok := stmt.(*ast.SelectStmt); ok {
found := findNestedContext(pass, inner, inner.Body.List) found := findNestedContext(pass, node, inner.Body.List)
if found != nil { if found != nil {
return found return found
} }
@ -167,7 +167,7 @@ func findNestedContext(pass *analysis.Pass, node ast.Node, stmts []ast.Stmt) *as
} }
if assignStmt.Tok == token.DEFINE { if assignStmt.Tok == token.DEFINE {
break continue
} }
// allow assignment to non-pointer children of values defined within the loop // allow assignment to non-pointer children of values defined within the loop

View file

@ -71,6 +71,14 @@ func example() {
ctx = wrapContext(ctx) ctx = wrapContext(ctx)
} }
} }
for {
ctx2 := context.Background()
ctx = wrapContext(ctx) // want "nested context in loop"
if doSomething() != nil {
ctx2 = wrapContext(ctx2)
}
}
} }
func wrapContext(ctx context.Context) context.Context { func wrapContext(ctx context.Context) context.Context {