sqlclosecheck icon indicating copy to clipboard operation
sqlclosecheck copied to clipboard

Feature Request: support close inside function call

Open dandandy opened this issue 5 years ago • 7 comments

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

dandandy avatar Aug 19 '20 23:08 dandandy

I will see what I can do.

ryanrolds avatar Oct 25 '20 19:10 ryanrolds

Is there any estimate when this can be fixed?

vovanec avatar Nov 16 '20 18:11 vovanec

big +1 this would help us too, we are wrapping the Close function in a helper function with signature db.CloseResource(ctx, resource)

urgas9 avatar Mar 08 '21 15:03 urgas9

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.

ocket8888 avatar May 19 '21 19:05 ocket8888

I will spend some time on this project over the next few weeks. I will take a look at this.

ryanrolds avatar Aug 20 '23 20:08 ryanrolds