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

(cont) Add support for deferred closer functions which are not methods #34

Merged
merged 1 commit into from
Nov 4, 2023
Merged
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
22 changes: 21 additions & 1 deletion pkg/analyzer/defer_only.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,27 @@ func getAction(instr ssa.Instruction, targetTypes []any) action {
return actionHandled
}
case *ssa.Return:
return actionReturned
if len(instr.Results) != 0 {
for _, result := range instr.Results {
resultType := result.Type()
for _, targetType := range targetTypes {
var tt types.Type

switch t := targetType.(type) {
case *types.Pointer:
tt = t
case *types.Named:
tt = t
default:
continue
}

if types.Identical(resultType, tt) {
return actionReturned
}
}
}
}
}

return actionUnhandled
Expand Down
1 change: 1 addition & 0 deletions testdata/sqlx_examples/expected_results.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
testdata/sqlx_examples/failure_generics.go:6:21: Rows/Stmt/NamedStmt was not closed
testdata/sqlx_examples/failure_generics.go:13:21: Rows/Stmt/NamedStmt was not closed
testdata/sqlx_examples/missing_close.go:10:24: Rows/Stmt/NamedStmt was not closed
testdata/sqlx_examples/missing_close_in_other_func.go:17:26: Rows/Stmt/NamedStmt was not closed
testdata/sqlx_examples/missing_close_named_stmt.go:8:30: Rows/Stmt/NamedStmt was not closed
testdata/sqlx_examples/non_defer_close.go:30:12: Close should use defer
25 changes: 25 additions & 0 deletions testdata/sqlx_examples/missing_close_in_other_func.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package sqlx_examples

import (
"fmt"
"log"

"github.com/jmoiron/sqlx"
)

func ForgetCloseSqlxStmt(stmt *sqlx.Stmt) {
if stmt != nil {
// stmt.Close()
}
}

func (s Server) DeferForgetCloseInOtherFunc() {
stmt, err := db.Preparex("SELECT 1")

Check failure on line 17 in testdata/sqlx_examples/missing_close_in_other_func.go

View workflow job for this annotation

GitHub Actions / CI

Rows/Stmt/NamedStmt was not closed
if err != nil {
log.Fatal(err)
}
defer ForgetCloseSqlxStmt(stmt)

rows := stmt.QueryRow()
fmt.Printf("%v", rows)
}
Loading