Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions testdata/src/contextbackground/basic/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"runtime"
"strings"
"testing"
"time"
)

func bar() func(t *testing.T) {
Expand Down Expand Up @@ -209,3 +210,22 @@ func Test_SwitchStmt_Tag(t *testing.T) {
func foobar() {
context.Background()
}

func Test_Ignores_Inside_Cleanup(t *testing.T) {
fakeSlowClose := func(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(time.Second):
return nil
}
}
t.Cleanup(func() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()
err := fakeSlowClose(ctx)
if err != nil {
t.Errorf("fakeSlowClose timed out on closing during cleanup: %v", err)
}
})
}
20 changes: 20 additions & 0 deletions testdata/src/contextbackground/basic/basic_test.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"runtime"
"strings"
"testing"
"time"
)

func bar() func(t *testing.T) {
Expand Down Expand Up @@ -209,3 +210,22 @@ func Test_SwitchStmt_Tag(t *testing.T) {
func foobar() {
context.Background()
}

func Test_Ignores_Inside_Cleanup(t *testing.T) {
fakeSlowClose := func(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(time.Second):
return nil
}
}
t.Cleanup(func() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()
err := fakeSlowClose(ctx)
if err != nil {
t.Errorf("fakeSlowClose timed out on closing during cleanup: %v", err)
}
})
}
9 changes: 9 additions & 0 deletions usetesting.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ func (a *analyzer) checkFunc(pass *analysis.Pass, ft *ast.FuncType, block *ast.B
return !a.reportIdent(pass, v, fnInfo, geGo124)

case *ast.CallExpr:
// Don't check inside t.Cleanup() calls
if selx, ok := v.Fun.(*ast.SelectorExpr); ok {
if idt, ok := selx.X.(*ast.Ident); ok {
if selx.Sel.Name == "Cleanup" && idt.Name == fnInfo.ArgName {
return false
}
}
}

return !a.reportCallExpr(pass, v, fnInfo)
}

Expand Down