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

Rows assigned within a conditional fail to trigger "missing close" #11

Open
therealplato opened this issue Dec 21, 2020 · 1 comment
Open
Labels
bug Something isn't working

Comments

@therealplato
Copy link

Thank you for the tool. Here's a repro of an issue I encountered.

Expected: The linter warns me that I never closed this rows

Actual: Not detected

package sqlx_examples

import (
        "context"
        "database/sql"
        "log"
        "strings"
)

func conditionalRowsMissingClose(onlyAdults bool) {
        age := 21
        var (
                rows *sql.Rows
                err  error
        )
        if onlyAdults {
                rows, err = db.QueryContext(context.Background(), "SELECT name FROM users WHERE age >= ?", age)
        } else {
                rows, err = db.QueryContext(context.Background(), "SELECT name FROM users", age)
        }
        if err != nil {
                log.Fatal(err)
        }
        // defer rows.Close()

        names := make([]string, 0)
        for rows.Next() {
                var name string
                if err := rows.Scan(&name); err != nil {
                        log.Fatal(err)
                }
                names = append(names, name)
        }

        // Check for errors from iterating over rows.
        if err := rows.Err(); err != nil {
                log.Fatal(err)
        }
        log.Printf("%s are at least %d years old", strings.Join(names, ", "), age)
}
@ryanrolds
Copy link
Owner

Thank you for the test case. I am adding to my list of things to look at. No hard ETA, hoping to have some time of the next couple of weeks.

@ryanrolds ryanrolds added the bug Something isn't working label Aug 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants