Skip to content

Commit

Permalink
enable forcetypeassert linter
Browse files Browse the repository at this point in the history
  • Loading branch information
korovindenis committed Apr 14, 2024
1 parent 0a476c3 commit 503a8f7
Show file tree
Hide file tree
Showing 30 changed files with 315 additions and 66 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ linters:
- exhaustivestruct
- exhaustruct
- forbidigo
- forcetypeassert
- funlen
- gochecknoglobals
- gocognit
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* Added type assertion checks to enhance type safety and prevent unexpected panics in critical sections of the codebase

## v3.65.0
* Supported OAuth 2.0 Token Exchange credentials provider

Expand Down
8 changes: 7 additions & 1 deletion internal/allocator/allocator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package allocator

import (
"fmt"
"sync"

"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
Expand Down Expand Up @@ -1107,7 +1108,12 @@ func (p *Pool[T]) Get() *T {
v = &zero
}

return v.(*T)
val, ok := v.(*T)
if !ok {
panic(fmt.Sprintf("assertion failed: expected type *T, got %T", v))
}

return val
}

func (p *Pool[T]) Put(t *T) {
Expand Down
6 changes: 5 additions & 1 deletion internal/bind/numeric_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ func (m NumericArgs) RewriteQuery(sql string, args ...interface{}) (yql string,
)
}
paramIndex := int(p - 1)
buffer.WriteString(newArgs[paramIndex].(table.ParameterOption).Name())
val, ok := newArgs[paramIndex].(table.ParameterOption)
if !ok {
panic(fmt.Sprintf("unsupported type conversion from %T to table.ParameterOption", val))
}
buffer.WriteString(val.Name())
}
}

Expand Down
15 changes: 12 additions & 3 deletions internal/cmd/gtrace/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ func (w *Writer) init() {
}

func (w *Writer) mustDeclare(name string) {
s := w.scope.Back().Value.(*scope)
s, ok := w.scope.Back().Value.(*scope)
if !ok {
panic(fmt.Sprintf("unsupported type conversion from %T to w.scope.Back()", s))
}
if !s.set(name) {
where := s.where(name)
panic(fmt.Sprintf(
Expand All @@ -100,7 +103,10 @@ func (w *Writer) declare(name string) string {
if isPredeclared(name) {
name = firstChar(name)
}
s := w.scope.Back().Value.(*scope)
s, ok := w.scope.Back().Value.(*scope)
if !ok {
panic(fmt.Sprintf("unsupported type conversion from %T to *scope", s))
}
for i := 0; ; i++ {
v := name
if i > 0 {
Expand All @@ -127,7 +133,10 @@ func (w *Writer) isGlobalScope() bool {
}

func (w *Writer) capture(vars ...string) {
s := w.scope.Back().Value.(*scope)
s, ok := w.scope.Back().Value.(*scope)
if !ok {
panic(fmt.Sprintf("unsupported type conversion from %T to *scope", s))
}
for _, v := range vars {
if !s.set(v) {
panic(fmt.Sprintf("can't capture variable %q", v))
Expand Down
6 changes: 5 additions & 1 deletion internal/conn/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,11 @@ func getContextMark(ctx context.Context) *modificationMark {
return &modificationMark{}
}

return v.(*modificationMark)
val, ok := v.(*modificationMark)
if !ok {
panic(fmt.Sprintf("unsupported type conversion from %T to *modificationMark", val))
}
return val

Check failure on line 572 in internal/conn/conn.go

View workflow job for this annotation

GitHub Actions / golangci-lint

return with no blank line before (nlreturn)
}

type modificationMark struct {
Expand Down
15 changes: 13 additions & 2 deletions internal/credentials/oauth2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,19 @@ func TestJWTTokenSource(t *testing.T) {
require.NoError(t, parsedToken.Claims.Valid())
require.Equal(t, "test_issuer", claims.Issuer)
require.Equal(t, "test_audience", claims.Audience[0])
require.Equal(t, "key_id", parsedToken.Header["kid"].(string))
require.Equal(t, "RS256", parsedToken.Header["alg"].(string))

val, ok := parsedToken.Header["kid"].(string)
if !ok {
panic(fmt.Sprintf("unsupported type conversion from %T to string", val))
}
require.Equal(t, "key_id", val)

val, ok = parsedToken.Header["alg"].(string)
if !ok {
panic(fmt.Sprintf("unsupported type conversion from %T to string", val))
}
require.Equal(t, "RS256", val)

Check failure on line 415 in internal/credentials/oauth2_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofumpt`-ed (gofumpt)
}

func TestJWTTokenBadParams(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rawtopicwriter

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -72,7 +73,11 @@ func TestSendWriteRequest(t *testing.T) {
}

getWriteRequest := func(req *Ydb_Topic.StreamWriteMessage_FromClient) *Ydb_Topic.StreamWriteMessage_WriteRequest {
return req.GetClientMessage().(*Ydb_Topic.StreamWriteMessage_FromClient_WriteRequest).WriteRequest
res, ok := req.ClientMessage.(*Ydb_Topic.StreamWriteMessage_FromClient_WriteRequest)

Check failure on line 76 in internal/grpcwrapper/rawtopic/rawtopicwriter/streamwriter_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

avoid direct access to proto field req.ClientMessage, use req.GetClientMessage() instead (protogetter)
if !ok {
panic(fmt.Sprintf("unsupported type conversion from %T to *Ydb_Topic.StreamWriteMessage_FromClient_WriteRequest", res))

Check failure on line 78 in internal/grpcwrapper/rawtopic/rawtopicwriter/streamwriter_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

line is 123 characters (lll)
}
return res.WriteRequest

Check failure on line 80 in internal/grpcwrapper/rawtopic/rawtopicwriter/streamwriter_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

return with no blank line before (nlreturn)
}

sendCounter := 0
Expand Down
25 changes: 20 additions & 5 deletions internal/table/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,12 +615,18 @@ func (c *Client) Close(ctx context.Context) (err error) {
c.limit = 0

for el := c.waitQ.Front(); el != nil; el = el.Next() {
ch := el.Value.(*chan *session)
ch, ok := el.Value.(*chan *session)
if !ok {
panic(fmt.Sprintf("unsupported type conversion from %T to *chan *session", ch))
}
close(*ch)
}

for e := c.idle.Front(); e != nil; e = e.Next() {
s := e.Value.(*session)
s, ok := e.Value.(*session)
if !ok {
panic(fmt.Sprintf("unsupported type conversion from %T to *session", s))
}
s.SetStatus(table.SessionClosing)
c.wg.Add(1)
go func() {
Expand Down Expand Up @@ -745,7 +751,10 @@ func (c *Client) internalPoolGCTick(ctx context.Context, idleThreshold time.Dura
return
}
for e := c.idle.Front(); e != nil; e = e.Next() {
s := e.Value.(*session)
s, ok := e.Value.(*session)
if !ok {
panic(fmt.Sprintf("unsupported type conversion from %T to *session", s))
}
info, has := c.index[s]
if !has {
panic("session not found in pool")
Expand Down Expand Up @@ -818,7 +827,10 @@ func (c *Client) internalPoolPeekFirstIdle() (s *session, touched time.Time) {
if el == nil {
return
}
s = el.Value.(*session)
s, ok := el.Value.(*session)
if !ok {
panic(fmt.Sprintf("unsupported type conversion from %T to *session", s))
}
info, has := c.index[s]
if !has || el != info.idle {
panic("inconsistent session client index")
Expand Down Expand Up @@ -857,7 +869,10 @@ func (c *Client) internalPoolNotify(s *session) (notified bool) {
// missed something and may want to retry (especially for case (3)).
//
// After that we taking a next waiter and repeat the same.
ch := c.waitQ.Remove(el).(*chan *session)
ch, ok := c.waitQ.Remove(el).(*chan *session)
if !ok {
panic(fmt.Sprintf("unsupported type conversion from %T to *chan *session", ch))
}
select {
case *ch <- s:
// Case (1).
Expand Down
5 changes: 4 additions & 1 deletion internal/table/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,10 @@ func (s *StubBuilder) createSession(ctx context.Context) (session *session, err
func (c *Client) debug() {
fmt.Print("head ")
for el := c.idle.Front(); el != nil; el = el.Next() {
s := el.Value.(*session)
s, ok := el.Value.(*session)
if !ok {
panic(fmt.Sprintf("unsupported type conversion from %T to *session", s))
}
x := c.index[s]
fmt.Printf("<-> %s(%d) ", s.ID(), x.touched.Unix())
}
Expand Down
6 changes: 5 additions & 1 deletion internal/table/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,11 @@ func TestRetryerSessionClosing(t *testing.T) {
config.New(),
func(ctx context.Context, s table.Session) error {
sessions = append(sessions, s)
s.(*session).SetStatus(table.SessionClosing)
val, ok := s.(*session)
if !ok {
panic(fmt.Sprintf("unsupported type conversion from %T to *session", val))
}
val.SetStatus(table.SessionClosing)

return nil
},
Expand Down
22 changes: 19 additions & 3 deletions internal/table/scanner/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,18 +264,34 @@ func TestNewStreamWithRecvFirstResultSet(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, result)
require.EqualValues(t, 1, tt.recvCounter)
require.EqualValues(t, 1, result.(*streamResult).nextResultSetCounter.Load())

val, ok := result.(*streamResult)
if !ok {
panic(fmt.Sprintf("unsupported type conversion from %T to *streamResult", val))
}
require.EqualValues(t, 1, val.nextResultSetCounter.Load())

for i := range make([]struct{}, 1000) {
err = result.NextResultSetErr(tt.ctx)
require.NoError(t, err)
require.Equal(t, i+1, tt.recvCounter)
require.Equal(t, i+2, int(result.(*streamResult).nextResultSetCounter.Load()))

val, ok := result.(*streamResult)

Check failure on line 279 in internal/table/scanner/result_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

shadow: declaration of "val" shadows declaration at line 268 (govet)
if !ok {
panic(fmt.Sprintf("unsupported type conversion from %T to *streamResult", val))
}
require.Equal(t, i+2, int(val.nextResultSetCounter.Load()))
}
err = result.NextResultSetErr(tt.ctx)
require.ErrorIs(t, err, io.EOF)
require.True(t, err == io.EOF) //nolint:errorlint,testifylint
require.Equal(t, 1001, tt.recvCounter)
require.Equal(t, 1002, int(result.(*streamResult).nextResultSetCounter.Load()))

val, ok = result.(*streamResult)
if !ok {
panic(fmt.Sprintf("unsupported type conversion from %T to *streamResult", val))
}
require.Equal(t, 1002, int(val.nextResultSetCounter.Load()))
}
})
}
Expand Down
7 changes: 5 additions & 2 deletions internal/table/scanner/scan_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scanner

import (
"bytes"
"fmt"
"io"
"reflect"
"strconv"
Expand Down Expand Up @@ -584,8 +585,10 @@ func (s *rawConverter) IsDecimal() bool {
}

func isEqualDecimal(d *Ydb.DecimalType, t types.Type) bool {
w := t.(*types.Decimal)

w, ok := t.(*types.Decimal)
if !ok {
panic(fmt.Sprintf("unsupported type conversion from %T to *types.Decimal", w))
}
return d.GetPrecision() == w.Precision() && d.GetScale() == w.Scale()

Check failure on line 592 in internal/table/scanner/scan_raw.go

View workflow job for this annotation

GitHub Actions / golangci-lint

return with no blank line before (nlreturn)
}

Expand Down
6 changes: 5 additions & 1 deletion internal/table/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,11 @@ func TestCreateTableRegression(t *testing.T) {
"attr": "attr_value",
},
}
if !proto.Equal(exp, act.(proto.Message)) {
val, ok := act.(proto.Message)
if !ok {
panic(fmt.Sprintf("unsupported type conversion from %T to proto.Message", val))
}
if !proto.Equal(exp, val) {
//nolint:revive
return nil, fmt.Errorf("proto's not equal: \n\nact: %v\n\nexp: %s\n\n", act, exp)
}
Expand Down
7 changes: 6 additions & 1 deletion internal/table/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,15 @@ func (tx *transaction) ExecuteStatement(
a := allocator.New()
defer a.Free()

val, ok := stmt.(*statement)
if !ok {
panic(fmt.Sprintf("unsupported type conversion from %T to *statement", val))
}

onDone := trace.TableOnTxExecuteStatement(
tx.s.config.Trace(), &ctx,
stack.FunctionID("github.com/ydb-platform/ydb-go-sdk/3/internal/table.(*transaction).ExecuteStatement"),
tx.s, tx, stmt.(*statement).query, parameters,
tx.s, tx, val.query, parameters,
)
defer func() {
onDone(r, err)
Expand Down
8 changes: 7 additions & 1 deletion internal/topic/topicreaderinternal/batcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package topicreaderinternal
import (
"context"
"errors"
"fmt"
"sync/atomic"
"testing"
"time"
Expand Down Expand Up @@ -339,10 +340,15 @@ func TestBatcherConcurency(t *testing.T) {
for i := 0; i < count; i++ {
res, err := b.Pop(ctx, batcherGetOptions{MinCount: 1})
require.NoError(tb, err)

val, ok := res.RawMessage.(*rawtopicreader.StartPartitionSessionRequest)
if !ok {
panic(fmt.Sprintf("unsupported type conversion from %T to *rawtopicreader.StartPartitionSessionRequest", val))
}
require.Equal(
tb,
rawtopicreader.NewOffset(int64(i)),
res.RawMessage.(*rawtopicreader.StartPartitionSessionRequest).CommittedOffset,
val.CommittedOffset,
)
}
})
Expand Down
17 changes: 14 additions & 3 deletions internal/topic/topicreaderinternal/committer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package topicreaderinternal
import (
"context"
"errors"
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -231,7 +232,11 @@ func TestCommitterBuffer(t *testing.T) {
c.clock = clock
c.BufferTimeLagTrigger = time.Second
c.send = func(msg rawtopicreader.ClientMessage) error {
commitMess := msg.(*rawtopicreader.CommitOffsetRequest)
commitMess, ok := msg.(*rawtopicreader.CommitOffsetRequest)
if !ok {
panic(fmt.Sprintf("unsupported type conversion from %T to *rawtopicreader.CommitOffsetRequest", commitMess))
}

require.Len(t, commitMess.CommitOffsets, 2)
close(sendCalled)

Expand Down Expand Up @@ -264,7 +269,10 @@ func TestCommitterBuffer(t *testing.T) {
c.BufferTimeLagTrigger = time.Second // for prevent send
c.BufferCountTrigger = 2
c.send = func(msg rawtopicreader.ClientMessage) error {
commitMess := msg.(*rawtopicreader.CommitOffsetRequest)
commitMess, ok := msg.(*rawtopicreader.CommitOffsetRequest)
if !ok {
panic(fmt.Sprintf("unsupported type conversion from %T to *rawtopicreader.CommitOffsetRequest", commitMess))
}
require.Len(t, commitMess.CommitOffsets, 4)
close(sendCalled)

Expand Down Expand Up @@ -299,7 +307,10 @@ func TestCommitterBuffer(t *testing.T) {
c.BufferTimeLagTrigger = time.Second // for prevent send
c.BufferCountTrigger = 4
c.send = func(msg rawtopicreader.ClientMessage) error {
commitMess := msg.(*rawtopicreader.CommitOffsetRequest)
commitMess, ok := msg.(*rawtopicreader.CommitOffsetRequest)
if !ok {
panic(fmt.Sprintf("unsupported type conversion from %T to *rawtopicreader.CommitOffsetRequest", commitMess))
}
require.Len(t, commitMess.CommitOffsets, 4)
close(sendCalled)

Expand Down
8 changes: 6 additions & 2 deletions internal/topic/topicwriterinternal/writer_grpc_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,12 @@ func (t *topicWriterOperationUnavailable) StreamWrite(server Ydb_Topic_V1.TopicS
return errors.New("failed to read messages block")
}

if len(messagesMsg.GetClientMessage().(*Ydb_Topic.StreamWriteMessage_FromClient_WriteRequest).
WriteRequest.GetMessages()) == 0 {
val, ok := messagesMsg.GetClientMessage().(*Ydb_Topic.StreamWriteMessage_FromClient_WriteRequest)
if !ok {
panic(fmt.Sprintf("unsupported type conversion from %T to *Ydb_Topic.StreamWriteMessage_FromClient_WriteRequest", val))

Check failure on line 106 in internal/topic/topicwriterinternal/writer_grpc_mock_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

line is 121 characters (lll)
}

if len(val.WriteRequest.GetMessages()) == 0 {
return errors.New("received zero messages block")
}

Expand Down
Loading

0 comments on commit 503a8f7

Please sign in to comment.