ryanrolds / sqlclosecheck

Linter that confirms DB rows and statements are closed properly.
MIT License
81 stars 14 forks source link

False positive: db.Queryx #35

Open pellared opened 5 months ago

pellared commented 5 months ago

Version: v0.5.1

Repro steps:

package bug_test

import (
    "log"

    "github.com/jmoiron/sqlx"
)

func Example() {
    db, err := sqlx.Open("pgx", "postgres://localhost/db")
    if err != nil {
        log.Print(err)
        return
    }
    defer db.Close()

    rows, err := db.Queryx("SELECT * FROM users")
    if err != nil {
        log.Print(err)
        return
    }
    defer rows.Close()
}

Result (from golangci-lint):

example_test.go:17:24: Rows/Stmt/NamedStmt was not closed (sqlclosecheck)
        rows, err := db.Queryx("SELECT * FROM users")
shubhamzanwar commented 5 months ago

➕1

we're facing the same issue in our workflows

ryanrolds commented 5 months ago

Thank you for the report. I will take a look at this over the next few days.

shubhamzanwar commented 4 months ago

How's it looking with this one? 🙏🏽

Bodyancheq commented 4 months ago

+1 in my project following code worked as a solution:

rows, err := db.Queryx("SELECT * FROM users")
if err != nil {
  log.Print(err)
  return
}
defer func() {
  _ = rows.Close()
}()

also worth mentioning: when I used sql instead of sqlx, there was no error

ivanovaleksey commented 2 weeks ago

Hi @ryanrolds ! Is there any progress on this issue?