Skip to content

Commit 931af3d

Browse files
committed
chore: improve rows.Err linter check
1 parent 3af8bd0 commit 931af3d

File tree

5 files changed

+14
-50
lines changed

5 files changed

+14
-50
lines changed

.golangci.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ linters:
4848
disabled: true
4949
rowserrcheck:
5050
packages:
51-
- "github.com/jmoiron/sqlx"
52-
- "github.com/jackc/pgx"
51+
- "github.com/jackc/pgx/v5"
5352
exclusions:
5453
generated: "lax"
5554
presets:

internal/datastore/crdb/pool/pool.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,11 @@ func (p *RetryPool) QueryFunc(ctx context.Context, rowsFunc func(ctx context.Con
172172
return err
173173
}
174174
defer rows.Close()
175-
return rowsFunc(ctx, rows)
175+
err = rowsFunc(ctx, rows)
176+
if err != nil {
177+
return err
178+
}
179+
return rows.Err()
176180
})
177181
}
178182

internal/datastore/postgres/common/interceptor.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (q querierInterceptor) Exec(ctx context.Context, sql string, arguments ...a
5959
}
6060

6161
func (q querierInterceptor) Query(ctx context.Context, sql string, arguments ...any) (pgx.Rows, error) {
62-
return q.interceptor.InterceptQuery(ctx, q.delegate, sql, arguments...)
62+
return q.interceptor.InterceptQuery(ctx, q.delegate, sql, arguments...) // nolint:rowserrcheck
6363
}
6464

6565
func (q querierInterceptor) QueryRow(ctx context.Context, sql string, arguments ...any) pgx.Row {
@@ -119,7 +119,7 @@ func (t txInterceptor) Exec(ctx context.Context, sql string, args ...any) (comma
119119
}
120120

121121
func (t txInterceptor) Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error) {
122-
return t.interceptor.InterceptQuery(ctx, t.delegate, sql, args...)
122+
return t.interceptor.InterceptQuery(ctx, t.delegate, sql, args...) // nolint:rowserrcheck
123123
}
124124

125125
func (t txInterceptor) QueryRow(ctx context.Context, sql string, args ...any) pgx.Row {
@@ -155,7 +155,7 @@ func (i InterceptorPooler) Exec(ctx context.Context, sql string, arguments ...an
155155
}
156156

157157
func (i InterceptorPooler) Query(ctx context.Context, sql string, optionsAndArgs ...any) (pgx.Rows, error) {
158-
return i.interceptingQuerier.Query(ctx, sql, optionsAndArgs...)
158+
return i.interceptingQuerier.Query(ctx, sql, optionsAndArgs...) // nolint:rowserrcheck
159159
}
160160

161161
func (i InterceptorPooler) QueryRow(ctx context.Context, sql string, optionsAndArgs ...any) pgx.Row {

internal/datastore/postgres/common/pgx.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,11 @@ func (t *QuerierFuncs) QueryFunc(ctx context.Context, rowsFunc func(ctx context.
278278
return err
279279
}
280280
defer rows.Close()
281-
return rowsFunc(ctx, rows)
281+
err = rowsFunc(ctx, rows)
282+
if err != nil {
283+
return err
284+
}
285+
return rows.Err()
282286
}
283287

284288
func (t *QuerierFuncs) QueryRowFunc(ctx context.Context, rowFunc func(ctx context.Context, row pgx.Row) error, sql string, optionsAndArgs ...any) error {

tools/analyzers/iferrafterrowclosecheck/iferrafterrowclosecheck.go

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package iferrafterrowclosecheck
33
import (
44
"flag"
55
"go/ast"
6-
"slices"
76
"strings"
87

98
"github.com/samber/lo"
@@ -12,11 +11,6 @@ import (
1211
"golang.org/x/tools/go/ast/inspector"
1312
)
1413

15-
type nodeAndStack struct {
16-
node ast.Node
17-
stack []ast.Node
18-
}
19-
2014
func Analyzer() *analysis.Analyzer {
2115
flagSet := flag.NewFlagSet("iferrafterrowclosecheck", flag.ExitOnError)
2216
skip := flagSet.String("skip-pkg", "", "package(s) to skip for linting")
@@ -129,40 +123,3 @@ func hasImmediateErrorCheck(closeStmt *ast.ExprStmt, rowsName string, stack []as
129123

130124
return false
131125
}
132-
133-
func isStatementRightAfter(first nodeAndStack, second nodeAndStack) bool {
134-
containingStatement, firstIndex, secondIndex := findSharedContainingStatement(
135-
append(slices.Clone(first.stack), first.node),
136-
append(slices.Clone(second.stack), second.node),
137-
)
138-
139-
if containingStatement == nil {
140-
return false
141-
}
142-
143-
return secondIndex == firstIndex+1
144-
}
145-
146-
func findSharedContainingStatement(first []ast.Node, second []ast.Node) (ast.Node, int, int) {
147-
for i := 0; i < min(len(first), len(second)); i++ {
148-
if i > 0 && first[i] != second[i] {
149-
for j := i - 1; j >= 0; j-- {
150-
if block, ok := first[j].(*ast.BlockStmt); ok {
151-
return block, stIndex(block.List, first[j+1]), stIndex(block.List, second[j+1])
152-
}
153-
154-
if cse, ok := first[j].(*ast.CaseClause); ok {
155-
return cse, stIndex(cse.Body, first[j+1]), stIndex(cse.Body, second[j+1])
156-
}
157-
}
158-
}
159-
}
160-
161-
return nil, -1, -1
162-
}
163-
164-
func stIndex(statements []ast.Stmt, node ast.Node) int {
165-
return slices.IndexFunc(statements, func(current ast.Stmt) bool {
166-
return current == node
167-
})
168-
}

0 commit comments

Comments
 (0)