Skip to content

Commit 6b70cbe

Browse files
committed
chore(lint): re-enable a small amount of go-critic
...and fix warnings
1 parent 584b270 commit 6b70cbe

11 files changed

+77
-67
lines changed

.golangci.yml

+11-6
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,18 @@ linters-settings:
2323
gocritic:
2424
enabled-tags:
2525
- diagnostic
26-
- experimental
27-
- opinionated
28-
- performance
29-
- style
26+
# - experimental
27+
# - opinionated
28+
# - performance
29+
# - style
3030
disabled-checks:
31-
- wrapperFunc
31+
- assignOp
32+
- appendAssign
33+
- commentedOutCode
3234
- ifElseChain
35+
- singleCaseSwitch
36+
- sloppyReassign
37+
- wrapperFunc
3338
funlen:
3439
lines: 300
3540
statements: 300
@@ -48,7 +53,7 @@ linters:
4853
- funlen
4954
- gochecknoinits
5055
# - goconst
51-
# - gocritic
56+
- gocritic
5257
- gocyclo
5358
- gofmt
5459
- goimports

admin_test.go

+45-43
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func TestClusterAdminListTopics(t *testing.T) {
161161
t.Fatal(err)
162162
}
163163

164-
if len(entries) <= 0 {
164+
if len(entries) == 0 {
165165
t.Fatal(errors.New("no resource present"))
166166
}
167167

@@ -447,10 +447,10 @@ func TestClusterAdminListPartitionReassignments(t *testing.T) {
447447
partitionStatus, ok := response["my_topic"]
448448
if !ok {
449449
t.Fatalf("topic missing in response")
450-
} else {
451-
if len(partitionStatus) != 2 {
452-
t.Fatalf("partition missing in response")
453-
}
450+
}
451+
452+
if len(partitionStatus) != 2 {
453+
t.Fatalf("partition missing in response")
454454
}
455455

456456
err = admin.Close()
@@ -711,47 +711,49 @@ func TestClusterAdminDescribeConfig(t *testing.T) {
711711
{V2_0_0_0, 2, true},
712712
}
713713
for _, tt := range tests {
714-
config := NewTestConfig()
715-
config.Version = tt.saramaVersion
716-
admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
717-
if err != nil {
718-
t.Fatal(err)
719-
}
720-
defer func() {
721-
_ = admin.Close()
722-
}()
723-
724-
resource := ConfigResource{
725-
Name: "r1",
726-
Type: TopicResource,
727-
ConfigNames: []string{"my_topic"},
728-
}
714+
t.Run(tt.saramaVersion.String(), func(t *testing.T) {
715+
config := NewTestConfig()
716+
config.Version = tt.saramaVersion
717+
admin, err := NewClusterAdmin([]string{seedBroker.Addr()}, config)
718+
if err != nil {
719+
t.Fatal(err)
720+
}
721+
defer func() {
722+
_ = admin.Close()
723+
}()
724+
725+
resource := ConfigResource{
726+
Name: "r1",
727+
Type: TopicResource,
728+
ConfigNames: []string{"my_topic"},
729+
}
729730

730-
entries, err := admin.DescribeConfig(resource)
731-
if err != nil {
732-
t.Fatal(err)
733-
}
731+
entries, err := admin.DescribeConfig(resource)
732+
if err != nil {
733+
t.Fatal(err)
734+
}
734735

735-
history := seedBroker.History()
736-
describeReq, ok := history[len(history)-1].Request.(*DescribeConfigsRequest)
737-
if !ok {
738-
t.Fatal("failed to find DescribeConfigsRequest in mockBroker history")
739-
}
736+
history := seedBroker.History()
737+
describeReq, ok := history[len(history)-1].Request.(*DescribeConfigsRequest)
738+
if !ok {
739+
t.Fatal("failed to find DescribeConfigsRequest in mockBroker history")
740+
}
740741

741-
if describeReq.Version != tt.requestVersion {
742-
t.Fatalf(
743-
"requestVersion %v did not match expected %v",
744-
describeReq.Version, tt.requestVersion)
745-
}
742+
if describeReq.Version != tt.requestVersion {
743+
t.Fatalf(
744+
"requestVersion %v did not match expected %v",
745+
describeReq.Version, tt.requestVersion)
746+
}
746747

747-
if len(entries) <= 0 {
748-
t.Fatal(errors.New("no resource present"))
749-
}
750-
if tt.includeSynonyms {
751-
if len(entries[0].Synonyms) == 0 {
752-
t.Fatal("expected synonyms to have been included")
748+
if len(entries) == 0 {
749+
t.Fatal(errors.New("no resource present"))
753750
}
754-
}
751+
if tt.includeSynonyms {
752+
if len(entries[0].Synonyms) == 0 {
753+
t.Fatal("expected synonyms to have been included")
754+
}
755+
}
756+
})
755757
}
756758
}
757759

@@ -829,7 +831,7 @@ func TestClusterAdminDescribeBrokerConfig(t *testing.T) {
829831
t.Fatal(err)
830832
}
831833

832-
if len(entries) <= 0 {
834+
if len(entries) == 0 {
833835
t.Fatal(errors.New("no resource present"))
834836
}
835837
}
@@ -1245,7 +1247,7 @@ func TestClusterAdminListAcls(t *testing.T) {
12451247
if err != nil {
12461248
t.Fatal(err)
12471249
}
1248-
if len(rAcls) <= 0 {
1250+
if len(rAcls) == 0 {
12491251
t.Fatal("no acls present")
12501252
}
12511253

async_producer_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ func TestAsyncProducerMultipleRetriesWithBackoffFunc(t *testing.T) {
684684

685685
// https://github.com/Shopify/sarama/issues/2129
686686
func TestAsyncProducerMultipleRetriesWithConcurrentRequests(t *testing.T) {
687-
//Logger = log.New(os.Stdout, "[sarama] ", log.LstdFlags)
687+
// Logger = log.New(os.Stdout, "[sarama] ", log.LstdFlags)
688688
seedBroker := NewMockBroker(t, 1)
689689
leader := NewMockBroker(t, 2)
690690

broker.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1407,7 +1407,7 @@ func (b *Broker) sendAndReceiveSASLSCRAMv0() error {
14071407
// Will be decremented in updateIncomingCommunicationMetrics (except error)
14081408
b.addRequestInFlightMetrics(1)
14091409
length := len(msg)
1410-
authBytes := make([]byte, length+4) //4 byte length header + auth data
1410+
authBytes := make([]byte, length+4) // 4 byte length header + auth data
14111411
binary.BigEndian.PutUint32(authBytes, uint32(length))
14121412
copy(authBytes[4:], []byte(msg))
14131413
_, err := b.write(authBytes)

client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ type client struct {
135135

136136
lock sync.RWMutex // protects access to the maps that hold cluster state.
137137

138-
updateMetaDataMs int64 //store update metadata time
138+
updateMetaDataMs int64 // store update metadata time
139139
}
140140

141141
// NewClient creates a new Client. It connects to one of the given broker addresses

delete_offsets_response.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
)
66

77
type DeleteOffsetsResponse struct {
8-
//The top-level error code, or 0 if there was no error.
8+
// The top-level error code, or 0 if there was no error.
99
ErrorCode KError
1010
ThrottleTime time.Duration
11-
//The responses for each partition of the topics.
11+
// The responses for each partition of the topics.
1212
Errors map[string]map[int32]KError
1313
}
1414

functional_consumer_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -553,12 +553,12 @@ func consumeMsgs(t *testing.T, clientVersions []KafkaVersion, producedMessages [
553553
if err != nil {
554554
t.Fatal(err)
555555
}
556-
defer safeClose(t, c)
556+
defer safeClose(t, c) //nolint: gocritic // the close intentionally happens outside the loop
557557
pc, err := c.ConsumePartition("test.1", 0, producedMessages[0].Offset)
558558
if err != nil {
559559
t.Fatal(err)
560560
}
561-
defer safeClose(t, pc)
561+
defer safeClose(t, pc) //nolint: gocritic // the close intentionally happens outside the loop
562562

563563
var wg sync.WaitGroup
564564
wg.Add(1)

mocks/async_producer.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,8 @@ func NewAsyncProducer(t ErrorReporter, config *sarama.Config) *AsyncProducer {
8585
msg.Offset = mp.lastOffset
8686
mp.successes <- msg
8787
}
88-
} else {
89-
if config.Producer.Return.Errors {
90-
mp.errors <- &sarama.ProducerError{Err: expectation.Result, Msg: msg}
91-
}
88+
} else if config.Producer.Return.Errors {
89+
mp.errors <- &sarama.ProducerError{Err: expectation.Result, Msg: msg}
9290
}
9391
}
9492
}

offset_commit_response_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var (
2828
)
2929

3030
func TestEmptyOffsetCommitResponse(t *testing.T) {
31-
//groupInstanceId := "gid"
31+
// groupInstanceId := "gid"
3232
tests := []struct {
3333
CaseName string
3434
Version int16

partitioner_test.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ func ExamplePartitioner_random() {
247247

248248
producer, err := NewSyncProducer([]string{"localhost:9092"}, config)
249249
if err != nil {
250-
log.Fatal(err)
250+
log.Println(err)
251+
return
251252
}
252253
defer func() {
253254
if err := producer.Close(); err != nil {
@@ -258,7 +259,8 @@ func ExamplePartitioner_random() {
258259
msg := &ProducerMessage{Topic: "test", Key: StringEncoder("key is set"), Value: StringEncoder("test")}
259260
partition, offset, err := producer.SendMessage(msg)
260261
if err != nil {
261-
log.Fatalln("Failed to produce message to kafka cluster.")
262+
log.Println("Failed to produce message to kafka cluster.")
263+
return
262264
}
263265

264266
log.Printf("Produced message to partition %d with offset %d", partition, offset)
@@ -273,7 +275,8 @@ func ExamplePartitioner_manual() {
273275

274276
producer, err := NewSyncProducer([]string{"localhost:9092"}, config)
275277
if err != nil {
276-
log.Fatal(err)
278+
log.Println(err)
279+
return
277280
}
278281
defer func() {
279282
if err := producer.Close(); err != nil {
@@ -286,11 +289,13 @@ func ExamplePartitioner_manual() {
286289

287290
partition, offset, err := producer.SendMessage(msg)
288291
if err != nil {
289-
log.Fatalln("Failed to produce message to kafka cluster.")
292+
log.Println("Failed to produce message to kafka cluster.")
293+
return
290294
}
291295

292296
if partition != 6 {
293-
log.Fatal("Message should have been produced to partition 6!")
297+
log.Println("Message should have been produced to partition 6!")
298+
return
294299
}
295300

296301
log.Printf("Produced message to partition %d with offset %d", partition, offset)

records.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func magicValue(pd packetDecoder) (int8, error) {
203203
}
204204

205205
func (r *Records) getControlRecord() (ControlRecord, error) {
206-
if r.RecordBatch == nil || len(r.RecordBatch.Records) <= 0 {
206+
if r.RecordBatch == nil || len(r.RecordBatch.Records) == 0 {
207207
return ControlRecord{}, fmt.Errorf("cannot get control record, record batch is empty")
208208
}
209209

0 commit comments

Comments
 (0)