diff --git a/analyzer/analyzer.go b/analyzer/analyzer.go index 020e650..ef54c4b 100644 --- a/analyzer/analyzer.go +++ b/analyzer/analyzer.go @@ -122,11 +122,11 @@ func (ta *ttempdirAnalyzer) checkSingleStmt(reporterBuilder ReporterBuilder, case *ast.IfStmt: ta.checkIfStmt(reporterBuilder, stmt) case *ast.AssignStmt: - reporter := reporterBuilder.Build(stmt.Pos()) - - ta.checkAssignStmt(reporter, stmt) + ta.checkAssignStmt(reporterBuilder.Build(stmt.Pos()), stmt) case *ast.ForStmt: ta.checkForStmt(reporterBuilder, stmt) + case *ast.DeferStmt: + ta.checkDeferStmt(reporterBuilder, stmt) } } @@ -134,10 +134,7 @@ func (ta *ttempdirAnalyzer) checkExprStmt(reporterBuilder ReporterBuilder, stmt *ast.ExprStmt, ) { if callExpr, ok := stmt.X.(*ast.CallExpr); ok { - ta.checkCallExprRecursive(reporterBuilder, - callExpr, - ta.maxRecursionLevel, - ) + ta.checkCallExpr(reporterBuilder, callExpr) } } @@ -183,12 +180,27 @@ func (ta *ttempdirAnalyzer) checkAssignStmt(reporter Reporter, } } +func (ta *ttempdirAnalyzer) checkDeferStmt(reporterBuilder ReporterBuilder, + stmt *ast.DeferStmt, +) { + ta.checkCallExpr(reporterBuilder, stmt.Call) +} + func (ta *ttempdirAnalyzer) checkForStmt(reporterBuilder ReporterBuilder, stmt *ast.ForStmt, ) { ta.checkStmts(reporterBuilder, stmt.Body.List) } +func (ta *ttempdirAnalyzer) checkCallExpr(reporterBuilder ReporterBuilder, + callExpr *ast.CallExpr, +) { + ta.checkCallExprRecursive(reporterBuilder, + callExpr, + ta.maxRecursionLevel, + ) +} + func (ta *ttempdirAnalyzer) checkFunctionExpr(reporter Reporter, functionExpr ast.Expr, ) { diff --git a/analyzer/testdata/src/a/a.go b/analyzer/testdata/src/a/a.go index dc53d80..5628e07 100644 --- a/analyzer/testdata/src/a/a.go +++ b/analyzer/testdata/src/a/a.go @@ -26,6 +26,15 @@ func F(t *testing.T) { if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in F" _ = err } + + _ = func(t *testing.T) { + _ = t + _, _ = os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in anonymous function" + } + + t.Cleanup(func() { + _, _ = os.MkdirTemp("a", "b") + }) } func BF(b *testing.B) { @@ -36,6 +45,11 @@ func BF(b *testing.B) { if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `b\\.TempDir\\(\\)` in BF" _ = err } + + func(b *testing.B) { + _ = b + _, _ = os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `b\\.TempDir\\(\\)` in anonymous function" + }(b) } func TBF(tb testing.TB) { @@ -45,6 +59,11 @@ func TBF(tb testing.TB) { if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in TBF" _ = err } + + defer func(tb testing.TB) { + _ = tb + _, _ = os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `tb\\.TempDir\\(\\)` in anonymous function" + }(tb) } func FF(f *testing.F) { @@ -54,4 +73,6 @@ func FF(f *testing.F) { if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FF" _ = err } + + defer os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `f\\.TempDir\\(\\)` in FF" } diff --git a/analyzer/testdata/src/d/d_test.go b/analyzer/testdata/src/d/d_test.go index fe1ceb2..9ec307e 100644 --- a/analyzer/testdata/src/d/d_test.go +++ b/analyzer/testdata/src/d/d_test.go @@ -26,6 +26,9 @@ func TestF(t *testing.T) { if _, err := os.MkdirTemp("a", "b"); err != nil { // want "os\\.MkdirTemp\\(\\) should be replaced by `t\\.TempDir\\(\\)` in TestF" _ = err } + t.Cleanup(func() { + _, _ = os.MkdirTemp("a", "b") // want "os\\.MkdirTemp\\(\\) should be replaced by `testing\\.TempDir\\(\\)` in anonymous function" + }) } func BenchmarkF(b *testing.B) {