Skip to content

Commit 22009c8

Browse files
golangci-lint: add sqlclosecheck linter and fixes (#18844)
Signed-off-by: Tim Vaillancourt <[email protected]>
1 parent c257ad6 commit 22009c8

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

.golangci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ linters:
1111
- govet
1212
- ineffassign
1313
- perfsprint
14+
- sqlclosecheck
1415
- staticcheck
1516
- whitespace
1617
settings:
@@ -155,6 +156,7 @@ linters:
155156
path: ^go/stats/statsd/
156157
- linters:
157158
- errcheck
159+
- sqlclosecheck
158160
path: ^go/test/
159161
- linters:
160162
- errcheck

go/vt/vitessdriver/driver_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ func TestTx(t *testing.T) {
569569
if err != nil {
570570
t.Fatal(err)
571571
}
572+
defer s.Close()
572573

573574
_, err = s.Exec(int64(0))
574575
if err != nil {
@@ -594,10 +595,12 @@ func TestTx(t *testing.T) {
594595
if err != nil {
595596
t.Fatal(err)
596597
}
597-
_, err = s.Query(int64(0))
598+
defer s.Close()
599+
r, err := s.Query(int64(0))
598600
if err != nil {
599601
t.Fatal(err)
600602
}
603+
defer r.Close()
601604
err = tx.Rollback()
602605
if err != nil {
603606
t.Fatal(err)
@@ -648,6 +651,7 @@ func TestSessionToken(t *testing.T) {
648651
if err != nil {
649652
t.Fatal(err)
650653
}
654+
defer s.Close()
651655

652656
_, err = s.Exec(int64(0))
653657
if err != nil {
@@ -674,6 +678,7 @@ func TestSessionToken(t *testing.T) {
674678
if err != nil {
675679
t.Fatal(err)
676680
}
681+
defer newS.Close()
677682

678683
_, err = newS.Exec(int64(1))
679684
if err != nil {

go/vt/vtadmin/cluster/cluster.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,7 @@ func (c *Cluster) getTablets(ctx context.Context) ([]*vtadminpb.Tablet, error) {
13181318
if err != nil {
13191319
return nil, err
13201320
}
1321+
defer rows.Close()
13211322

13221323
return c.parseTablets(rows)
13231324
}

go/vt/vtadmin/vtsql/vtsql.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ func (vtgate *VTGateProxy) ShowTablets(ctx context.Context) (*sql.Rows, error) {
178178

179179
vtadminproto.AnnotateClusterSpan(vtgate.cluster, span)
180180

181-
return vtgate.conn.QueryContext(vtgate.getQueryContext(ctx), "SHOW vitess_tablets")
181+
// The caller must run .Close() if the *sql.Rows != nil. We will suppress the sqlclosecheck linter here.
182+
return vtgate.conn.QueryContext(vtgate.getQueryContext(ctx), "SHOW vitess_tablets") //nolint:sqlclosecheck
182183
}
183184

184185
// VExplain is part of the DB interface.
@@ -189,10 +190,11 @@ func (vtgate *VTGateProxy) VExplain(ctx context.Context, query string, vexplainS
189190
vtadminproto.AnnotateClusterSpan(vtgate.cluster, span)
190191

191192
rows, err := vtgate.conn.QueryContext(vtgate.getQueryContext(ctx), query)
192-
193193
if err != nil {
194194
return nil, err
195195
}
196+
defer rows.Close()
197+
196198
switch vexplainStmt.Type {
197199
case sqlparser.QueriesVExplainType:
198200
return convertVExplainQueriesResultToString(rows)

0 commit comments

Comments
 (0)