Fix staticcheck issues

pull/1708/head
Jack Christensen 2023-07-28 18:04:26 -05:00
parent 8fb309c631
commit 1a9b2a53a5
8 changed files with 34 additions and 37 deletions

View File

@ -507,7 +507,7 @@ func (c *Conn) execSimpleProtocol(ctx context.Context, sql string, arguments []a
mrr := c.pgConn.Exec(ctx, sql) mrr := c.pgConn.Exec(ctx, sql)
for mrr.NextResult() { for mrr.NextResult() {
commandTag, err = mrr.ResultReader().Close() commandTag, _ = mrr.ResultReader().Close()
} }
err = mrr.Close() err = mrr.Close()
return commandTag, err return commandTag, err

View File

@ -315,7 +315,7 @@ func TestExecContextWithoutCancelation(t *testing.T) {
defer cancel() defer cancel()
pgxtest.RunWithQueryExecModes(ctx, t, defaultConnTestRunner, nil, func(ctx context.Context, t testing.TB, conn *pgx.Conn) { pgxtest.RunWithQueryExecModes(ctx, t, defaultConnTestRunner, nil, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
ctx, cancelFunc := context.WithCancel(context.Background()) ctx, cancelFunc := context.WithCancel(ctx)
defer cancelFunc() defer cancelFunc()
commandTag, err := conn.Exec(ctx, "create temporary table foo(id integer primary key);") commandTag, err := conn.Exec(ctx, "create temporary table foo(id integer primary key);")
@ -336,7 +336,7 @@ func TestExecContextFailureWithoutCancelation(t *testing.T) {
defer cancel() defer cancel()
pgxtest.RunWithQueryExecModes(ctx, t, defaultConnTestRunner, nil, func(ctx context.Context, t testing.TB, conn *pgx.Conn) { pgxtest.RunWithQueryExecModes(ctx, t, defaultConnTestRunner, nil, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
ctx, cancelFunc := context.WithCancel(context.Background()) ctx, cancelFunc := context.WithCancel(ctx)
defer cancelFunc() defer cancelFunc()
_, err := conn.Exec(ctx, "selct;") _, err := conn.Exec(ctx, "selct;")
@ -361,7 +361,7 @@ func TestExecContextFailureWithoutCancelationWithArguments(t *testing.T) {
defer cancel() defer cancel()
pgxtest.RunWithQueryExecModes(ctx, t, defaultConnTestRunner, nil, func(ctx context.Context, t testing.TB, conn *pgx.Conn) { pgxtest.RunWithQueryExecModes(ctx, t, defaultConnTestRunner, nil, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
ctx, cancelFunc := context.WithCancel(context.Background()) ctx, cancelFunc := context.WithCancel(ctx)
defer cancelFunc() defer cancelFunc()
_, err := conn.Exec(ctx, "selct $1;", 1) _, err := conn.Exec(ctx, "selct $1;", 1)
@ -565,6 +565,7 @@ func TestListenNotify(t *testing.T) {
defer cancel() defer cancel()
notification, err = listener.WaitForNotification(ctx) notification, err = listener.WaitForNotification(ctx)
assert.True(t, pgconn.Timeout(err)) assert.True(t, pgconn.Timeout(err))
assert.Nil(t, notification)
// listener can listen again after a timeout // listener can listen again after a timeout
mustExec(t, notifier, "notify chat") mustExec(t, notifier, "notify chat")

View File

@ -656,16 +656,9 @@ func TestQueryRowCoreIntegerEncoding(t *testing.T) {
defer closeConn(t, conn) defer closeConn(t, conn)
type allTypes struct { type allTypes struct {
ui uint i16 int16
ui8 uint8 i32 int32
ui16 uint16 i64 int64
ui32 uint32
ui64 uint64
i int
i8 int8
i16 int16
i32 int32
i64 int64
} }
var actual, zero allTypes var actual, zero allTypes
@ -983,7 +976,6 @@ func TestQueryRowErrors(t *testing.T) {
type allTypes struct { type allTypes struct {
i16 int16 i16 int16
i int
s string s string
} }

View File

@ -306,7 +306,7 @@ func (rows *baseRows) Values() ([]any, error) {
copy(newBuf, buf) copy(newBuf, buf)
values = append(values, newBuf) values = append(values, newBuf)
default: default:
rows.fatal(errors.New("Unknown format code")) rows.fatal(errors.New("unknown format code"))
} }
} }

View File

@ -270,6 +270,7 @@ func TestCollectOneRowPrefersPostgreSQLErrorOverErrNoRows(t *testing.T) {
var pgErr *pgconn.PgError var pgErr *pgconn.PgError
require.ErrorAs(t, err, &pgErr) require.ErrorAs(t, err, &pgErr)
require.Equal(t, "23505", pgErr.Code) require.Equal(t, "23505", pgErr.Code)
require.Equal(t, "", name)
}) })
} }

View File

@ -24,6 +24,8 @@ type testTracer struct {
traceConnectEnd func(ctx context.Context, data pgx.TraceConnectEndData) traceConnectEnd func(ctx context.Context, data pgx.TraceConnectEndData)
} }
type ctxKey string
func (tt *testTracer) TraceQueryStart(ctx context.Context, conn *pgx.Conn, data pgx.TraceQueryStartData) context.Context { func (tt *testTracer) TraceQueryStart(ctx context.Context, conn *pgx.Conn, data pgx.TraceQueryStartData) context.Context {
if tt.traceQueryStart != nil { if tt.traceQueryStart != nil {
return tt.traceQueryStart(ctx, conn, data) return tt.traceQueryStart(ctx, conn, data)
@ -117,13 +119,13 @@ func TestTraceExec(t *testing.T) {
require.Equal(t, `select $1::text`, data.SQL) require.Equal(t, `select $1::text`, data.SQL)
require.Len(t, data.Args, 1) require.Len(t, data.Args, 1)
require.Equal(t, `testing`, data.Args[0]) require.Equal(t, `testing`, data.Args[0])
return context.WithValue(ctx, "fromTraceQueryStart", "foo") return context.WithValue(ctx, ctxKey(ctxKey("fromTraceQueryStart")), "foo")
} }
traceQueryEndCalled := false traceQueryEndCalled := false
tracer.traceQueryEnd = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceQueryEndData) { tracer.traceQueryEnd = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceQueryEndData) {
traceQueryEndCalled = true traceQueryEndCalled = true
require.Equal(t, "foo", ctx.Value("fromTraceQueryStart")) require.Equal(t, "foo", ctx.Value(ctxKey(ctxKey("fromTraceQueryStart"))))
require.Equal(t, `SELECT 1`, data.CommandTag.String()) require.Equal(t, `SELECT 1`, data.CommandTag.String())
require.NoError(t, data.Err) require.NoError(t, data.Err)
} }
@ -157,13 +159,13 @@ func TestTraceQuery(t *testing.T) {
require.Equal(t, `select $1::text`, data.SQL) require.Equal(t, `select $1::text`, data.SQL)
require.Len(t, data.Args, 1) require.Len(t, data.Args, 1)
require.Equal(t, `testing`, data.Args[0]) require.Equal(t, `testing`, data.Args[0])
return context.WithValue(ctx, "fromTraceQueryStart", "foo") return context.WithValue(ctx, ctxKey("fromTraceQueryStart"), "foo")
} }
traceQueryEndCalled := false traceQueryEndCalled := false
tracer.traceQueryEnd = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceQueryEndData) { tracer.traceQueryEnd = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceQueryEndData) {
traceQueryEndCalled = true traceQueryEndCalled = true
require.Equal(t, "foo", ctx.Value("fromTraceQueryStart")) require.Equal(t, "foo", ctx.Value(ctxKey("fromTraceQueryStart")))
require.Equal(t, `SELECT 1`, data.CommandTag.String()) require.Equal(t, `SELECT 1`, data.CommandTag.String())
require.NoError(t, data.Err) require.NoError(t, data.Err)
} }
@ -198,20 +200,20 @@ func TestTraceBatchNormal(t *testing.T) {
traceBatchStartCalled = true traceBatchStartCalled = true
require.NotNil(t, data.Batch) require.NotNil(t, data.Batch)
require.Equal(t, 2, data.Batch.Len()) require.Equal(t, 2, data.Batch.Len())
return context.WithValue(ctx, "fromTraceBatchStart", "foo") return context.WithValue(ctx, ctxKey("fromTraceBatchStart"), "foo")
} }
traceBatchQueryCalledCount := 0 traceBatchQueryCalledCount := 0
tracer.traceBatchQuery = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchQueryData) { tracer.traceBatchQuery = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchQueryData) {
traceBatchQueryCalledCount++ traceBatchQueryCalledCount++
require.Equal(t, "foo", ctx.Value("fromTraceBatchStart")) require.Equal(t, "foo", ctx.Value(ctxKey("fromTraceBatchStart")))
require.NoError(t, data.Err) require.NoError(t, data.Err)
} }
traceBatchEndCalled := false traceBatchEndCalled := false
tracer.traceBatchEnd = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchEndData) { tracer.traceBatchEnd = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchEndData) {
traceBatchEndCalled = true traceBatchEndCalled = true
require.Equal(t, "foo", ctx.Value("fromTraceBatchStart")) require.Equal(t, "foo", ctx.Value(ctxKey("fromTraceBatchStart")))
require.NoError(t, data.Err) require.NoError(t, data.Err)
} }
@ -261,20 +263,20 @@ func TestTraceBatchClose(t *testing.T) {
traceBatchStartCalled = true traceBatchStartCalled = true
require.NotNil(t, data.Batch) require.NotNil(t, data.Batch)
require.Equal(t, 2, data.Batch.Len()) require.Equal(t, 2, data.Batch.Len())
return context.WithValue(ctx, "fromTraceBatchStart", "foo") return context.WithValue(ctx, ctxKey("fromTraceBatchStart"), "foo")
} }
traceBatchQueryCalledCount := 0 traceBatchQueryCalledCount := 0
tracer.traceBatchQuery = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchQueryData) { tracer.traceBatchQuery = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchQueryData) {
traceBatchQueryCalledCount++ traceBatchQueryCalledCount++
require.Equal(t, "foo", ctx.Value("fromTraceBatchStart")) require.Equal(t, "foo", ctx.Value(ctxKey("fromTraceBatchStart")))
require.NoError(t, data.Err) require.NoError(t, data.Err)
} }
traceBatchEndCalled := false traceBatchEndCalled := false
tracer.traceBatchEnd = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchEndData) { tracer.traceBatchEnd = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchEndData) {
traceBatchEndCalled = true traceBatchEndCalled = true
require.Equal(t, "foo", ctx.Value("fromTraceBatchStart")) require.Equal(t, "foo", ctx.Value(ctxKey("fromTraceBatchStart")))
require.NoError(t, data.Err) require.NoError(t, data.Err)
} }
@ -312,13 +314,13 @@ func TestTraceBatchErrorWhileReadingResults(t *testing.T) {
traceBatchStartCalled = true traceBatchStartCalled = true
require.NotNil(t, data.Batch) require.NotNil(t, data.Batch)
require.Equal(t, 3, data.Batch.Len()) require.Equal(t, 3, data.Batch.Len())
return context.WithValue(ctx, "fromTraceBatchStart", "foo") return context.WithValue(ctx, ctxKey("fromTraceBatchStart"), "foo")
} }
traceBatchQueryCalledCount := 0 traceBatchQueryCalledCount := 0
tracer.traceBatchQuery = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchQueryData) { tracer.traceBatchQuery = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchQueryData) {
traceBatchQueryCalledCount++ traceBatchQueryCalledCount++
require.Equal(t, "foo", ctx.Value("fromTraceBatchStart")) require.Equal(t, "foo", ctx.Value(ctxKey("fromTraceBatchStart")))
if traceBatchQueryCalledCount == 2 { if traceBatchQueryCalledCount == 2 {
require.Error(t, data.Err) require.Error(t, data.Err)
} else { } else {
@ -329,7 +331,7 @@ func TestTraceBatchErrorWhileReadingResults(t *testing.T) {
traceBatchEndCalled := false traceBatchEndCalled := false
tracer.traceBatchEnd = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchEndData) { tracer.traceBatchEnd = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchEndData) {
traceBatchEndCalled = true traceBatchEndCalled = true
require.Equal(t, "foo", ctx.Value("fromTraceBatchStart")) require.Equal(t, "foo", ctx.Value(ctxKey("fromTraceBatchStart")))
require.Error(t, data.Err) require.Error(t, data.Err)
} }
@ -381,13 +383,13 @@ func TestTraceBatchErrorWhileReadingResultsWhileClosing(t *testing.T) {
traceBatchStartCalled = true traceBatchStartCalled = true
require.NotNil(t, data.Batch) require.NotNil(t, data.Batch)
require.Equal(t, 3, data.Batch.Len()) require.Equal(t, 3, data.Batch.Len())
return context.WithValue(ctx, "fromTraceBatchStart", "foo") return context.WithValue(ctx, ctxKey("fromTraceBatchStart"), "foo")
} }
traceBatchQueryCalledCount := 0 traceBatchQueryCalledCount := 0
tracer.traceBatchQuery = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchQueryData) { tracer.traceBatchQuery = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchQueryData) {
traceBatchQueryCalledCount++ traceBatchQueryCalledCount++
require.Equal(t, "foo", ctx.Value("fromTraceBatchStart")) require.Equal(t, "foo", ctx.Value(ctxKey("fromTraceBatchStart")))
if traceBatchQueryCalledCount == 2 { if traceBatchQueryCalledCount == 2 {
require.Error(t, data.Err) require.Error(t, data.Err)
} else { } else {
@ -398,7 +400,7 @@ func TestTraceBatchErrorWhileReadingResultsWhileClosing(t *testing.T) {
traceBatchEndCalled := false traceBatchEndCalled := false
tracer.traceBatchEnd = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchEndData) { tracer.traceBatchEnd = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchEndData) {
traceBatchEndCalled = true traceBatchEndCalled = true
require.Equal(t, "foo", ctx.Value("fromTraceBatchStart")) require.Equal(t, "foo", ctx.Value(ctxKey("fromTraceBatchStart")))
require.Error(t, data.Err) require.Error(t, data.Err)
} }
@ -440,13 +442,13 @@ func TestTraceCopyFrom(t *testing.T) {
traceCopyFromStartCalled = true traceCopyFromStartCalled = true
require.Equal(t, pgx.Identifier{"foo"}, data.TableName) require.Equal(t, pgx.Identifier{"foo"}, data.TableName)
require.Equal(t, []string{"a"}, data.ColumnNames) require.Equal(t, []string{"a"}, data.ColumnNames)
return context.WithValue(ctx, "fromTraceCopyFromStart", "foo") return context.WithValue(ctx, ctxKey("fromTraceCopyFromStart"), "foo")
} }
traceCopyFromEndCalled := false traceCopyFromEndCalled := false
tracer.traceCopyFromEnd = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceCopyFromEndData) { tracer.traceCopyFromEnd = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceCopyFromEndData) {
traceCopyFromEndCalled = true traceCopyFromEndCalled = true
require.Equal(t, "foo", ctx.Value("fromTraceCopyFromStart")) require.Equal(t, "foo", ctx.Value(ctxKey("fromTraceCopyFromStart")))
require.Equal(t, `COPY 2`, data.CommandTag.String()) require.Equal(t, `COPY 2`, data.CommandTag.String())
require.NoError(t, data.Err) require.NoError(t, data.Err)
} }
@ -488,7 +490,7 @@ func TestTracePrepare(t *testing.T) {
tracePrepareStartCalled = true tracePrepareStartCalled = true
require.Equal(t, `ps`, data.Name) require.Equal(t, `ps`, data.Name)
require.Equal(t, `select $1::text`, data.SQL) require.Equal(t, `select $1::text`, data.SQL)
return context.WithValue(ctx, "fromTracePrepareStart", "foo") return context.WithValue(ctx, ctxKey("fromTracePrepareStart"), "foo")
} }
tracePrepareEndCalled := false tracePrepareEndCalled := false
@ -530,7 +532,7 @@ func TestTraceConnect(t *testing.T) {
tracer.traceConnectStart = func(ctx context.Context, data pgx.TraceConnectStartData) context.Context { tracer.traceConnectStart = func(ctx context.Context, data pgx.TraceConnectStartData) context.Context {
traceConnectStartCalled = true traceConnectStartCalled = true
require.NotNil(t, data.ConnConfig) require.NotNil(t, data.ConnConfig)
return context.WithValue(ctx, "fromTraceConnectStart", "foo") return context.WithValue(ctx, ctxKey("fromTraceConnectStart"), "foo")
} }
traceConnectEndCalled := false traceConnectEndCalled := false

1
tx.go
View File

@ -152,7 +152,6 @@ type Tx interface {
// called on the dbTx. // called on the dbTx.
type dbTx struct { type dbTx struct {
conn *Conn conn *Conn
err error
savepointNum int64 savepointNum int64
closed bool closed bool
} }

View File

@ -555,6 +555,7 @@ func TestTxBeginFuncNestedTransactionCommit(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
return nil return nil
}) })
require.NoError(t, err)
return nil return nil
}) })
@ -601,6 +602,7 @@ func TestTxBeginFuncNestedTransactionRollback(t *testing.T) {
return nil return nil
}) })
require.NoError(t, err)
var n int64 var n int64
err = db.QueryRow(context.Background(), "select count(*) from foo").Scan(&n) err = db.QueryRow(context.Background(), "select count(*) from foo").Scan(&n)