sqlclosecheck
sqlclosecheck copied to clipboard
Feature Request: support close inside function call
I would like to be able call a function to close my sqlx.Stmt structs to reduce dublicate code using a function like:
func closer(aStruct io.Closer) {
err := aStruct.Close()
if err != nil {
logrus.Warnf("error closing sqlx statement %v", err)
}
}
statement, err := db.Preparex(findUserOptions)
defer closer(statement)
But the version of sqlclosecheck in golangci-lint v1.30 will throw an error because it thinks I'm not closing the struct
pkg/database/client.go:174:34: Rows/Stmt was not closed (sqlclosecheck) statement, err := c.db.Preparex(findUserOptions)
If you think this feature is possible and worthwhile I'm interested in working on it
I will see what I can do.
Is there any estimate when this can be fixed?
big +1 this would help us too, we are wrapping the Close function in a helper function with signature db.CloseResource(ctx, resource)
This would be nice for some code I maintain, because I, too, have a wrapper function - log.Close(resource) - and my choices are to either do:
rows, err := tx.Query(query)
if err != nil {
return err
}
defer rows.Close()
which causes a lint issue with errcheck because the error return isn't being checked, or:
rows, err := tx.Query(query)
if err != nil {
return err
}
defer log.Close(rows)
which causes this lint "issue" to appear.
I will spend some time on this project over the next few weeks. I will take a look at this.