Skip to content

Commit

Permalink
add support to defer statement (#12)
Browse files Browse the repository at this point in the history
* Bump codecov/codecov-action from 4.0.1 to 4.4.1

Bumps [codecov/codecov-action](https://github.com/codecov/codecov-action) from 4.0.1 to 4.4.1.
- [Release notes](https://github.com/codecov/codecov-action/releases)
- [Changelog](https://github.com/codecov/codecov-action/blob/main/CHANGELOG.md)
- [Commits](codecov/codecov-action@v4.0.1...v4.4.1)

---
updated-dependencies:
- dependency-name: codecov/codecov-action
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>

* Bump golang.org/x/tools in the production-dependencies group

Bumps the production-dependencies group with 1 update: [golang.org/x/tools](https://github.com/golang/tools).


Updates `golang.org/x/tools` from 0.21.0 to 0.22.0
- [Release notes](https://github.com/golang/tools/releases)
- [Commits](golang/tools@v0.21.0...v0.22.0)

---
updated-dependencies:
- dependency-name: golang.org/x/tools
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: production-dependencies
...

Signed-off-by: dependabot[bot] <[email protected]>

* refactor

* add doc

* must support golang for-loop statements

* change the danalyzer report message (#9)

change from "can be replaced" by "should be replaced"

* must support defer statement (#11)

* must support defer statement
* add test with t.Cleanup

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
  • Loading branch information
peczenyj and dependabot[bot] authored Jun 5, 2024
1 parent a5481d2 commit f5fcc18
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
26 changes: 19 additions & 7 deletions analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,19 @@ 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)
}
}

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)
}
}

Expand Down Expand Up @@ -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,
) {
Expand Down
21 changes: 21 additions & 0 deletions analyzer/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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"
}
3 changes: 3 additions & 0 deletions analyzer/testdata/src/d/d_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit f5fcc18

Please sign in to comment.