-
Notifications
You must be signed in to change notification settings - Fork 881
Lint when t.Cleanup closes over t #3237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jakebailey
wants to merge
3
commits into
main
Choose a base branch
from
jabaile/cleanup-t
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| package customlint | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "go/ast" | ||
| "go/types" | ||
|
|
||
| "golang.org/x/tools/go/analysis" | ||
| "golang.org/x/tools/go/analysis/passes/inspect" | ||
| "golang.org/x/tools/go/ast/inspector" | ||
| ) | ||
|
|
||
| var cleanupAnalyzer = &analysis.Analyzer{ | ||
| Name: "cleanup", | ||
| Doc: "finds t.Cleanup calls where the closure captures a testing variable from the enclosing function", | ||
| Requires: []*analysis.Analyzer{ | ||
| inspect.Analyzer, | ||
| }, | ||
| Run: func(pass *analysis.Pass) (any, error) { | ||
| return (&cleanupPass{pass: pass}).run() | ||
| }, | ||
| } | ||
|
|
||
| type cleanupPass struct { | ||
| pass *analysis.Pass | ||
| } | ||
|
|
||
| func (c *cleanupPass) run() (any, error) { | ||
| in := c.pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) | ||
|
|
||
| for cursor := range in.Root().Preorder((*ast.CallExpr)(nil)) { | ||
| call := cursor.Node().(*ast.CallExpr) | ||
| c.checkCall(call) | ||
| } | ||
|
|
||
| return nil, nil | ||
| } | ||
|
|
||
| func (c *cleanupPass) checkCall(call *ast.CallExpr) { | ||
| sel, ok := call.Fun.(*ast.SelectorExpr) | ||
| if !ok || sel.Sel.Name != "Cleanup" { | ||
| return | ||
| } | ||
|
|
||
| recvType := c.pass.TypesInfo.TypeOf(sel.X) | ||
| if recvType == nil || !isTestingType(recvType) { | ||
| return | ||
| } | ||
|
|
||
| if len(call.Args) != 1 { | ||
| return | ||
| } | ||
| funcLit, ok := call.Args[0].(*ast.FuncLit) | ||
| if !ok { | ||
| return | ||
| } | ||
|
|
||
| ast.Inspect(funcLit.Body, func(n ast.Node) bool { | ||
| c.checkCleanupIdent(n, funcLit) | ||
| return true | ||
| }) | ||
| } | ||
|
|
||
| func (c *cleanupPass) checkCleanupIdent(n ast.Node, funcLit *ast.FuncLit) { | ||
| ident, ok := n.(*ast.Ident) | ||
| if !ok { | ||
| return | ||
| } | ||
| obj := c.pass.TypesInfo.Uses[ident] | ||
| if obj == nil { | ||
| return | ||
| } | ||
| v, ok := obj.(*types.Var) | ||
| if !ok { | ||
| return | ||
| } | ||
| if !isTestingType(v.Type()) { | ||
| return | ||
| } | ||
| // Flag if the variable is defined outside the closure (captured). | ||
| if v.Pos() < funcLit.Pos() || v.Pos() >= funcLit.End() { | ||
| c.pass.Report(analysis.Diagnostic{ | ||
| Pos: ident.Pos(), | ||
| End: ident.End(), | ||
| Message: fmt.Sprintf("cleanup closure captures %s; the test will have ended when cleanup runs", ident.Name), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func isTestingType(t types.Type) bool { | ||
| if ptr, ok := t.(*types.Pointer); ok { | ||
| t = ptr.Elem() | ||
| } | ||
| named, ok := t.(*types.Named) | ||
| if !ok { | ||
| return false | ||
| } | ||
| obj := named.Obj() | ||
| if obj.Pkg() == nil || obj.Pkg().Path() != "testing" { | ||
| return false | ||
| } | ||
| switch obj.Name() { | ||
| case "T", "B", "F", "TB": | ||
| return true | ||
| } | ||
| return false | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package cleanup | ||
|
|
||
| import "testing" | ||
|
|
||
| func badT(t *testing.T) { | ||
| t.Cleanup(func() { | ||
| t.Log("done") | ||
| }) | ||
| } | ||
|
|
||
| func badB(b *testing.B) { | ||
| b.Cleanup(func() { | ||
| b.Log("done") | ||
| }) | ||
| } | ||
|
|
||
| func badTB(tb testing.TB) { | ||
| tb.Cleanup(func() { | ||
| tb.Log("done") | ||
| }) | ||
| } | ||
|
|
||
| func badMultipleRefs(t *testing.T) { | ||
| t.Cleanup(func() { | ||
| t.Log("first") | ||
| t.Log("second") | ||
| }) | ||
| } | ||
|
|
||
| func good(t *testing.T) { | ||
| t.Cleanup(func() { | ||
| println("no capture") | ||
| }) | ||
| } | ||
|
|
||
| func goodNamedFunc(t *testing.T) { | ||
| t.Cleanup(namedFunc) | ||
| } | ||
|
|
||
| func namedFunc() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package cleanup | ||
|
|
||
| import "testing" | ||
|
|
||
| func badT(t *testing.T) { | ||
| t.Cleanup(func() { | ||
| t.Log("done") | ||
| ~ | ||
| !!! cleanup: cleanup closure captures t; the test will have ended when cleanup runs | ||
| }) | ||
| } | ||
|
|
||
| func badB(b *testing.B) { | ||
| b.Cleanup(func() { | ||
| b.Log("done") | ||
| ~ | ||
| !!! cleanup: cleanup closure captures b; the test will have ended when cleanup runs | ||
| }) | ||
| } | ||
|
|
||
| func badTB(tb testing.TB) { | ||
| tb.Cleanup(func() { | ||
| tb.Log("done") | ||
| ~~ | ||
| !!! cleanup: cleanup closure captures tb; the test will have ended when cleanup runs | ||
| }) | ||
| } | ||
|
|
||
| func badMultipleRefs(t *testing.T) { | ||
| t.Cleanup(func() { | ||
| t.Log("first") | ||
| ~ | ||
| !!! cleanup: cleanup closure captures t; the test will have ended when cleanup runs | ||
| t.Log("second") | ||
| ~ | ||
| !!! cleanup: cleanup closure captures t; the test will have ended when cleanup runs | ||
| }) | ||
| } | ||
|
|
||
| func good(t *testing.T) { | ||
| t.Cleanup(func() { | ||
| println("no capture") | ||
| }) | ||
| } | ||
|
|
||
| func goodNamedFunc(t *testing.T) { | ||
| t.Cleanup(namedFunc) | ||
| } | ||
|
|
||
| func namedFunc() {} | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
closeClient()returns an error when the LSP server goroutines fail to shut down cleanly; ignoring it here can let test failures slip by silently. Consider handling the error without referencingt(e.g., fail-fast viapanicor other non-testing.Treporting mechanism) so cleanup issues still cause the test to fail while satisfying the new lint rule.