Skip to content
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

Fix for BatchReadOnlyTransaction check #48

Closed
wants to merge 1 commit into from
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
19 changes: 19 additions & 0 deletions passes/unclosetx/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,22 @@ func f6(ctx context.Context, client *spanner.Client) error {
}
return nil
}

func f7(ctx context.Context, client *spanner.Client) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test case which does not call Close method with a // want comment.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f5 is that the testcase.

func f5(ctx context.Context, client *spanner.Client) {
tx, _ := client.BatchReadOnlyTransaction(ctx, spanner.StrongRead()) // want "transaction must be closed"
_ = tx
}

ro, _ := client.BatchReadOnlyTransaction(ctx, spanner.StrongRead())
defer ro.Close()

stmt := spanner.Statement{SQL: `SELECT 1`}

iter := ro.Query(ctx, stmt)
defer iter.Stop()

for {
_, err := iter.Next()
if err != nil {
break
}
}

return nil
}
17 changes: 13 additions & 4 deletions passes/unclosetx/unclosetx.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,24 @@ func run(pass *analysis.Pass) (interface{}, error) {
funcs := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs
cmaps := pass.ResultOf[commentmap.Analyzer].(comment.Maps)

txTyp := zaganeutils.TypeOf(pass, "*ReadOnlyTransaction")
if txTyp == nil {
txTypeRo := zaganeutils.TypeOf(pass, "*ReadOnlyTransaction")
if txTypeRo == nil {
// skip checking
return nil, nil
}

txTypeBatch := zaganeutils.TypeOf(pass, "*BatchReadOnlyTransaction")
if txTypeBatch == nil {
// skip checking
return nil, nil
}

var methods []*types.Func
for _, s := range strings.Split(closeMethods, ",") {
if m := analysisutil.MethodOf(txTyp, s); m != nil {
if m := analysisutil.MethodOf(txTypeRo, s); m != nil {
methods = append(methods, m)
}
if m := analysisutil.MethodOf(txTypeBatch, s); m != nil {
methods = append(methods, m)
}
}
Expand All @@ -59,7 +68,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
// skip this
continue
}
instrs := analysisutil.NotCalledIn(f, txTyp, methods...)
instrs := analysisutil.NotCalledIn(f, txTypeRo, methods...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the second argument of NotCalledIn is a receiver of the methods, it cannot find txTypeBatch's methods.

I think you should call NotCalledIn two times for txTypRo and txTypeBatch and merge theses return values into instrs.

Copy link
Author

@sters sters Sep 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I use this:

instrs := append(
	analysisutil.NotCalledIn(f, txTypeRo, methods...),
	analysisutil.NotCalledIn(f, txTypeBatch, methods...)...,
)
for _, instr := range instrs {
...

The test says:

/my/go/path/root/sters/zagane/passes/unclosetx/analysistest.go:446: a/a.go:55:42: unexpected diagnostic: transaction must be closed

it points to this test case:

image

I have no idea to solve this test failure. 🤔

for _, instr := range instrs {
pos := instr.Pos()
if pos == token.NoPos {
Expand Down