Skip to content

Commit

Permalink
Fix staticcheck issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jackc committed Jul 28, 2023
1 parent 8fb309c commit 1a9b2a5
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 37 deletions.
2 changes: 1 addition & 1 deletion conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ func (c *Conn) execSimpleProtocol(ctx context.Context, sql string, arguments []a

mrr := c.pgConn.Exec(ctx, sql)
for mrr.NextResult() {
commandTag, err = mrr.ResultReader().Close()
commandTag, _ = mrr.ResultReader().Close()
}
err = mrr.Close()
return commandTag, err
Expand Down
7 changes: 4 additions & 3 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func TestExecContextWithoutCancelation(t *testing.T) {
defer cancel()

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()

commandTag, err := conn.Exec(ctx, "create temporary table foo(id integer primary key);")
Expand All @@ -336,7 +336,7 @@ func TestExecContextFailureWithoutCancelation(t *testing.T) {
defer cancel()

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()

_, err := conn.Exec(ctx, "selct;")
Expand All @@ -361,7 +361,7 @@ func TestExecContextFailureWithoutCancelationWithArguments(t *testing.T) {
defer cancel()

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()

_, err := conn.Exec(ctx, "selct $1;", 1)
Expand Down Expand Up @@ -565,6 +565,7 @@ func TestListenNotify(t *testing.T) {
defer cancel()
notification, err = listener.WaitForNotification(ctx)
assert.True(t, pgconn.Timeout(err))
assert.Nil(t, notification)

// listener can listen again after a timeout
mustExec(t, notifier, "notify chat")
Expand Down
14 changes: 3 additions & 11 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,16 +656,9 @@ func TestQueryRowCoreIntegerEncoding(t *testing.T) {
defer closeConn(t, conn)

type allTypes struct {
ui uint
ui8 uint8
ui16 uint16
ui32 uint32
ui64 uint64
i int
i8 int8
i16 int16
i32 int32
i64 int64
i16 int16
i32 int32
i64 int64
}

var actual, zero allTypes
Expand Down Expand Up @@ -983,7 +976,6 @@ func TestQueryRowErrors(t *testing.T) {

type allTypes struct {
i16 int16
i int
s string
}

Expand Down
2 changes: 1 addition & 1 deletion rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func (rows *baseRows) Values() ([]any, error) {
copy(newBuf, buf)
values = append(values, newBuf)
default:
rows.fatal(errors.New("Unknown format code"))
rows.fatal(errors.New("unknown format code"))
}
}

Expand Down
1 change: 1 addition & 0 deletions rows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ func TestCollectOneRowPrefersPostgreSQLErrorOverErrNoRows(t *testing.T) {
var pgErr *pgconn.PgError
require.ErrorAs(t, err, &pgErr)
require.Equal(t, "23505", pgErr.Code)
require.Equal(t, "", name)
})
}

Expand Down
42 changes: 22 additions & 20 deletions tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type testTracer struct {
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 {
if tt.traceQueryStart != nil {
return tt.traceQueryStart(ctx, conn, data)
Expand Down Expand Up @@ -117,13 +119,13 @@ func TestTraceExec(t *testing.T) {
require.Equal(t, `select $1::text`, data.SQL)
require.Len(t, data.Args, 1)
require.Equal(t, `testing`, data.Args[0])
return context.WithValue(ctx, "fromTraceQueryStart", "foo")
return context.WithValue(ctx, ctxKey(ctxKey("fromTraceQueryStart")), "foo")
}

traceQueryEndCalled := false
tracer.traceQueryEnd = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceQueryEndData) {
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.NoError(t, data.Err)
}
Expand Down Expand Up @@ -157,13 +159,13 @@ func TestTraceQuery(t *testing.T) {
require.Equal(t, `select $1::text`, data.SQL)
require.Len(t, data.Args, 1)
require.Equal(t, `testing`, data.Args[0])
return context.WithValue(ctx, "fromTraceQueryStart", "foo")
return context.WithValue(ctx, ctxKey("fromTraceQueryStart"), "foo")
}

traceQueryEndCalled := false
tracer.traceQueryEnd = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceQueryEndData) {
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.NoError(t, data.Err)
}
Expand Down Expand Up @@ -198,20 +200,20 @@ func TestTraceBatchNormal(t *testing.T) {
traceBatchStartCalled = true
require.NotNil(t, data.Batch)
require.Equal(t, 2, data.Batch.Len())
return context.WithValue(ctx, "fromTraceBatchStart", "foo")
return context.WithValue(ctx, ctxKey("fromTraceBatchStart"), "foo")
}

traceBatchQueryCalledCount := 0
tracer.traceBatchQuery = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchQueryData) {
traceBatchQueryCalledCount++
require.Equal(t, "foo", ctx.Value("fromTraceBatchStart"))
require.Equal(t, "foo", ctx.Value(ctxKey("fromTraceBatchStart")))
require.NoError(t, data.Err)
}

traceBatchEndCalled := false
tracer.traceBatchEnd = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchEndData) {
traceBatchEndCalled = true
require.Equal(t, "foo", ctx.Value("fromTraceBatchStart"))
require.Equal(t, "foo", ctx.Value(ctxKey("fromTraceBatchStart")))
require.NoError(t, data.Err)
}

Expand Down Expand Up @@ -261,20 +263,20 @@ func TestTraceBatchClose(t *testing.T) {
traceBatchStartCalled = true
require.NotNil(t, data.Batch)
require.Equal(t, 2, data.Batch.Len())
return context.WithValue(ctx, "fromTraceBatchStart", "foo")
return context.WithValue(ctx, ctxKey("fromTraceBatchStart"), "foo")
}

traceBatchQueryCalledCount := 0
tracer.traceBatchQuery = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchQueryData) {
traceBatchQueryCalledCount++
require.Equal(t, "foo", ctx.Value("fromTraceBatchStart"))
require.Equal(t, "foo", ctx.Value(ctxKey("fromTraceBatchStart")))
require.NoError(t, data.Err)
}

traceBatchEndCalled := false
tracer.traceBatchEnd = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchEndData) {
traceBatchEndCalled = true
require.Equal(t, "foo", ctx.Value("fromTraceBatchStart"))
require.Equal(t, "foo", ctx.Value(ctxKey("fromTraceBatchStart")))
require.NoError(t, data.Err)
}

Expand Down Expand Up @@ -312,13 +314,13 @@ func TestTraceBatchErrorWhileReadingResults(t *testing.T) {
traceBatchStartCalled = true
require.NotNil(t, data.Batch)
require.Equal(t, 3, data.Batch.Len())
return context.WithValue(ctx, "fromTraceBatchStart", "foo")
return context.WithValue(ctx, ctxKey("fromTraceBatchStart"), "foo")
}

traceBatchQueryCalledCount := 0
tracer.traceBatchQuery = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchQueryData) {
traceBatchQueryCalledCount++
require.Equal(t, "foo", ctx.Value("fromTraceBatchStart"))
require.Equal(t, "foo", ctx.Value(ctxKey("fromTraceBatchStart")))
if traceBatchQueryCalledCount == 2 {
require.Error(t, data.Err)
} else {
Expand All @@ -329,7 +331,7 @@ func TestTraceBatchErrorWhileReadingResults(t *testing.T) {
traceBatchEndCalled := false
tracer.traceBatchEnd = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchEndData) {
traceBatchEndCalled = true
require.Equal(t, "foo", ctx.Value("fromTraceBatchStart"))
require.Equal(t, "foo", ctx.Value(ctxKey("fromTraceBatchStart")))
require.Error(t, data.Err)
}

Expand Down Expand Up @@ -381,13 +383,13 @@ func TestTraceBatchErrorWhileReadingResultsWhileClosing(t *testing.T) {
traceBatchStartCalled = true
require.NotNil(t, data.Batch)
require.Equal(t, 3, data.Batch.Len())
return context.WithValue(ctx, "fromTraceBatchStart", "foo")
return context.WithValue(ctx, ctxKey("fromTraceBatchStart"), "foo")
}

traceBatchQueryCalledCount := 0
tracer.traceBatchQuery = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchQueryData) {
traceBatchQueryCalledCount++
require.Equal(t, "foo", ctx.Value("fromTraceBatchStart"))
require.Equal(t, "foo", ctx.Value(ctxKey("fromTraceBatchStart")))
if traceBatchQueryCalledCount == 2 {
require.Error(t, data.Err)
} else {
Expand All @@ -398,7 +400,7 @@ func TestTraceBatchErrorWhileReadingResultsWhileClosing(t *testing.T) {
traceBatchEndCalled := false
tracer.traceBatchEnd = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchEndData) {
traceBatchEndCalled = true
require.Equal(t, "foo", ctx.Value("fromTraceBatchStart"))
require.Equal(t, "foo", ctx.Value(ctxKey("fromTraceBatchStart")))
require.Error(t, data.Err)
}

Expand Down Expand Up @@ -440,13 +442,13 @@ func TestTraceCopyFrom(t *testing.T) {
traceCopyFromStartCalled = true
require.Equal(t, pgx.Identifier{"foo"}, data.TableName)
require.Equal(t, []string{"a"}, data.ColumnNames)
return context.WithValue(ctx, "fromTraceCopyFromStart", "foo")
return context.WithValue(ctx, ctxKey("fromTraceCopyFromStart"), "foo")
}

traceCopyFromEndCalled := false
tracer.traceCopyFromEnd = func(ctx context.Context, conn *pgx.Conn, data pgx.TraceCopyFromEndData) {
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.NoError(t, data.Err)
}
Expand Down Expand Up @@ -488,7 +490,7 @@ func TestTracePrepare(t *testing.T) {
tracePrepareStartCalled = true
require.Equal(t, `ps`, data.Name)
require.Equal(t, `select $1::text`, data.SQL)
return context.WithValue(ctx, "fromTracePrepareStart", "foo")
return context.WithValue(ctx, ctxKey("fromTracePrepareStart"), "foo")
}

tracePrepareEndCalled := false
Expand Down Expand Up @@ -530,7 +532,7 @@ func TestTraceConnect(t *testing.T) {
tracer.traceConnectStart = func(ctx context.Context, data pgx.TraceConnectStartData) context.Context {
traceConnectStartCalled = true
require.NotNil(t, data.ConnConfig)
return context.WithValue(ctx, "fromTraceConnectStart", "foo")
return context.WithValue(ctx, ctxKey("fromTraceConnectStart"), "foo")
}

traceConnectEndCalled := false
Expand Down
1 change: 0 additions & 1 deletion tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ type Tx interface {
// called on the dbTx.
type dbTx struct {
conn *Conn
err error
savepointNum int64
closed bool
}
Expand Down
2 changes: 2 additions & 0 deletions tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ func TestTxBeginFuncNestedTransactionCommit(t *testing.T) {
require.NoError(t, err)
return nil
})
require.NoError(t, err)

return nil
})
Expand Down Expand Up @@ -601,6 +602,7 @@ func TestTxBeginFuncNestedTransactionRollback(t *testing.T) {

return nil
})
require.NoError(t, err)

var n int64
err = db.QueryRow(context.Background(), "select count(*) from foo").Scan(&n)
Expand Down

0 comments on commit 1a9b2a5

Please sign in to comment.