This commit is contained in:
Gabriel Augendre 2025-01-13 22:58:49 +01:00 committed by GitHub
commit 462a741adc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 34 additions and 1 deletions
pkg/analyzer
testdata/src

View file

@ -169,6 +169,18 @@ func findNestedContext(pass *analysis.Pass, node ast.Node, stmts []ast.Stmt) *as
continue
}
// Ignore [context.Background] & [context.TODO].
if call, ok := assignStmt.Rhs[0].(*ast.CallExpr); ok {
if selector, ok := call.Fun.(*ast.SelectorExpr); ok {
ident, identOK := selector.X.(*ast.Ident)
if identOK &&
ident.Name == "context" &&
(selector.Sel.Name == "Background" || selector.Sel.Name == "TODO") {
continue
}
}
}
// allow assignment to non-pointer children of values defined within the loop
if lhs := getRootIdent(pass, assignStmt.Lhs[0]); lhs != nil {
if obj := pass.TypesInfo.ObjectOf(lhs); obj != nil {

View file

@ -1,6 +1,9 @@
package src
import "context"
import (
"context"
"testing"
)
func example() {
ctx := context.Background()
@ -228,3 +231,21 @@ func okMiddleware2(ctx context.Context) func(ctx context.Context) error {
func doSomethingWithCtx(ctx context.Context) error {
return nil
}
func testCasesInit(t *testing.T) {
cases := []struct {
ctx context.Context
}{
{},
{
ctx: context.WithValue(context.Background(), "key", "value"),
},
}
for _, tc := range cases {
t.Run("some test", func(t *testing.T) {
if tc.ctx == nil {
tc.ctx = context.Background()
}
})
}
}