mirror of
https://github.com/Crocmagnon/fatcontext.git
synced 2025-04-10 19:46:34 +02:00
feat: ignore context.TODO and context.Background
This commit is contained in:
parent
52f7fb588c
commit
41f5549490
2 changed files with 34 additions and 1 deletions
|
@ -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 {
|
||||
|
|
23
testdata/src/example.go
vendored
23
testdata/src/example.go
vendored
|
@ -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()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue