Skip to content

Commit 54e593c

Browse files
committedJan 14, 2025·
feat: ignore context.TODO and context.Background
Related to #34
1 parent 529e088 commit 54e593c

File tree

2 files changed

+70
-22
lines changed

2 files changed

+70
-22
lines changed
 

‎pkg/analyzer/analyzer.go

+48-21
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"go/ast"
88
"go/printer"
99
"go/token"
10-
"go/types"
10+
"slices"
1111

1212
"golang.org/x/tools/go/analysis"
1313
"golang.org/x/tools/go/analysis/passes/inspect"
@@ -169,13 +169,14 @@ func findNestedContext(pass *analysis.Pass, node ast.Node, stmts []ast.Stmt) *as
169169
continue
170170
}
171171

172+
// Ignore [context.Background] & [context.TODO].
173+
if isContextFunction(assignStmt.Rhs[0], "Background", "TODO") {
174+
continue
175+
}
176+
172177
// allow assignment to non-pointer children of values defined within the loop
173-
if lhs := getRootIdent(pass, assignStmt.Lhs[0]); lhs != nil {
174-
if obj := pass.TypesInfo.ObjectOf(lhs); obj != nil {
175-
if checkObjectScopeWithinNode(obj.Parent(), node) {
176-
continue // definition is within the loop
177-
}
178-
}
178+
if isWithinLoop(assignStmt.Lhs[0], node, pass) {
179+
continue
179180
}
180181

181182
return assignStmt
@@ -184,16 +185,51 @@ func findNestedContext(pass *analysis.Pass, node ast.Node, stmts []ast.Stmt) *as
184185
return nil
185186
}
186187

187-
func checkObjectScopeWithinNode(scope *types.Scope, node ast.Node) bool {
188-
if scope == nil {
188+
// render returns the pretty-print of the given node
189+
func render(fset *token.FileSet, x interface{}) ([]byte, error) {
190+
var buf bytes.Buffer
191+
if err := printer.Fprint(&buf, fset, x); err != nil {
192+
return nil, fmt.Errorf("printing node: %w", err)
193+
}
194+
return buf.Bytes(), nil
195+
}
196+
197+
func isContextFunction(exp ast.Expr, fnName ...string) bool {
198+
call, ok := exp.(*ast.CallExpr)
199+
if !ok {
200+
return false
201+
}
202+
203+
selector, ok := call.Fun.(*ast.SelectorExpr)
204+
if !ok {
205+
return false
206+
}
207+
208+
ident, ok := selector.X.(*ast.Ident)
209+
if !ok {
210+
return false
211+
}
212+
213+
return ident.Name == "context" && slices.Contains(fnName, selector.Sel.Name)
214+
}
215+
216+
func isWithinLoop(exp ast.Expr, node ast.Node, pass *analysis.Pass) bool {
217+
lhs := getRootIdent(pass, exp)
218+
if lhs == nil {
189219
return false
190220
}
191221

192-
if scope.Pos() >= node.Pos() && scope.End() <= node.End() {
193-
return true
222+
obj := pass.TypesInfo.ObjectOf(lhs)
223+
if obj == nil {
224+
return false
225+
}
226+
227+
scope := obj.Parent()
228+
if scope == nil {
229+
return false
194230
}
195231

196-
return false
232+
return scope.Pos() >= node.Pos() && scope.End() <= node.End()
197233
}
198234

199235
func getRootIdent(pass *analysis.Pass, node ast.Node) *ast.Ident {
@@ -213,12 +249,3 @@ func getRootIdent(pass *analysis.Pass, node ast.Node) *ast.Ident {
213249
}
214250
}
215251
}
216-
217-
// render returns the pretty-print of the given node
218-
func render(fset *token.FileSet, x interface{}) ([]byte, error) {
219-
var buf bytes.Buffer
220-
if err := printer.Fprint(&buf, fset, x); err != nil {
221-
return nil, fmt.Errorf("printing node: %w", err)
222-
}
223-
return buf.Bytes(), nil
224-
}

‎testdata/src/example.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package src
22

3-
import "context"
3+
import (
4+
"context"
5+
"testing"
6+
)
47

58
func example() {
69
ctx := context.Background()
@@ -228,3 +231,21 @@ func okMiddleware2(ctx context.Context) func(ctx context.Context) error {
228231
func doSomethingWithCtx(ctx context.Context) error {
229232
return nil
230233
}
234+
235+
func testCasesInit(t *testing.T) {
236+
cases := []struct {
237+
ctx context.Context
238+
}{
239+
{},
240+
{
241+
ctx: context.WithValue(context.Background(), "key", "value"),
242+
},
243+
}
244+
for _, tc := range cases {
245+
t.Run("some test", func(t *testing.T) {
246+
if tc.ctx == nil {
247+
tc.ctx = context.Background()
248+
}
249+
})
250+
}
251+
}

0 commit comments

Comments
 (0)
Please sign in to comment.