Skip to content

Commit 526bf08

Browse files
authored
Merge pull request #775 from fxamacker/fxamacker/update-golangci-lint
Add more linters to .golangci.yml and lint
2 parents a6b110a + 071cd93 commit 526bf08

7 files changed

Lines changed: 58 additions & 52 deletions

File tree

.golangci.yml

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,51 @@
1+
# .golangci.yml for https://github.com/fxamacker/cbor
2+
# Updated on May 10, 2026.
13
version: "2"
4+
5+
formatters:
6+
enable:
7+
- gofmt
8+
- goimports
9+
settings:
10+
goimports:
11+
local-prefixes:
12+
- github.com/fxamacker/cbor
13+
14+
issues:
15+
max-issues-per-linter: 0
16+
max-same-issues: 0
17+
218
linters:
319
default: none
420
enable:
521
- asciicheck
622
- bidichk
723
- depguard
24+
- dogsled
25+
- dupword
826
- errcheck
27+
- exptostd
928
- forbidigo
1029
- goconst
1130
- gocritic
1231
- gocyclo
32+
- godoclint
1333
- goprintffuncname
1434
- gosec
1535
- govet
1636
- ineffassign
37+
- intrange
1738
- misspell
18-
- nilerr
39+
- nilnesserr
40+
- perfsprint
41+
- predeclared
42+
- reassign
1943
- revive
2044
- staticcheck
2145
- unconvert
2246
- unused
47+
- usestdlibvars
48+
- wastedassign
2349
settings:
2450
depguard:
2551
rules:
@@ -31,9 +57,6 @@ linters:
3157
allow:
3258
- $gostd
3359
- github.com/x448/float16
34-
deny:
35-
- pkg: io/ioutil
36-
desc: 'replaced by io and os packages since Go 1.16: https://tip.golang.org/doc/go1.16#ioutil'
3760
dupl:
3861
threshold: 100
3962
funlen:
@@ -58,7 +81,12 @@ linters:
5881
- style
5982
govet:
6083
enable:
84+
- deepequalerrors
85+
- nilness
86+
- reflectvaluecompare
6187
- shadow
88+
- sortslice
89+
- unusedwrite
6290
lll:
6391
line-length: 140
6492
misspell:
@@ -95,22 +123,4 @@ linters:
95123
- third_party$
96124
- builtin$
97125
- examples$
98-
issues:
99-
max-issues-per-linter: 0
100-
max-same-issues: 0
101-
formatters:
102-
enable:
103-
- gofmt
104-
- goimports
105-
settings:
106-
gofmt:
107-
simplify: false
108-
goimports:
109-
local-prefixes:
110-
- github.com/fxamacker/cbor
111-
exclusions:
112-
generated: lax
113-
paths:
114-
- third_party$
115-
- builtin$
116-
- examples$
126+

bench_test.go

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ func BenchmarkUnmarshal(b *testing.B) {
288288
name = "CBOR " + bm.name + " to Go " + t.Kind().String()
289289
}
290290
b.Run(name, func(b *testing.B) {
291-
for i := 0; i < b.N; i++ {
291+
for b.Loop() {
292292
vPtr := reflect.New(t).Interface()
293293
if err := Unmarshal(bm.data, vPtr); err != nil {
294294
b.Fatal("Unmarshal:", err)
@@ -341,7 +341,7 @@ func BenchmarkUnmarshal(b *testing.B) {
341341
}
342342
for _, bm := range moreBenchmarks {
343343
b.Run(bm.name, func(b *testing.B) {
344-
for i := 0; i < b.N; i++ {
344+
for b.Loop() {
345345
vPtr := reflect.New(bm.decodeToType).Interface()
346346
if err := Unmarshal(bm.data, vPtr); err != nil {
347347
b.Fatal("Unmarshal:", err)
@@ -364,7 +364,7 @@ func BenchmarkUnmarshalFirst(b *testing.B) {
364364
data = append(data, bm.data...)
365365
data = append(data, trailingData...)
366366
b.Run(name, func(b *testing.B) {
367-
for i := 0; i < b.N; i++ {
367+
for b.Loop() {
368368
vPtr := reflect.New(t).Interface()
369369
if _, err := UnmarshalFirst(data, vPtr); err != nil {
370370
b.Fatal("UnmarshalFirst:", err)
@@ -388,7 +388,7 @@ func BenchmarkUnmarshalFirstViaDecoder(b *testing.B) {
388388
data = append(data, bm.data...)
389389
data = append(data, trailingData...)
390390
b.Run(name, func(b *testing.B) {
391-
for i := 0; i < b.N; i++ {
391+
for b.Loop() {
392392
vPtr := reflect.New(t).Interface()
393393
if err := NewDecoder(bytes.NewReader(data)).Decode(vPtr); err != nil {
394394
b.Fatal("UnmarshalDecoder:", err)
@@ -409,7 +409,7 @@ func BenchmarkDecode(b *testing.B) {
409409
buf := bytes.NewReader(bm.data)
410410
decoder := NewDecoder(buf)
411411
b.Run(name, func(b *testing.B) {
412-
for i := 0; i < b.N; i++ {
412+
for b.Loop() {
413413
vPtr := reflect.New(t).Interface()
414414
if err := decoder.Decode(vPtr); err != nil {
415415
b.Fatal("Decode:", err)
@@ -424,7 +424,7 @@ func BenchmarkDecode(b *testing.B) {
424424
func BenchmarkDecodeStream(b *testing.B) {
425425
var data []byte
426426
for _, bm := range decodeBenchmarks {
427-
for i := 0; i < len(bm.decodeToTypes); i++ {
427+
for range len(bm.decodeToTypes) {
428428
data = append(data, bm.data...)
429429
}
430430
}
@@ -454,7 +454,7 @@ func BenchmarkMarshal(b *testing.B) {
454454
name = "Go " + reflect.TypeOf(v).Kind().String() + " to CBOR " + bm.name
455455
}
456456
b.Run(name, func(b *testing.B) {
457-
for i := 0; i < b.N; i++ {
457+
for b.Loop() {
458458
if _, err := Marshal(v); err != nil {
459459
b.Fatal("Marshal:", err)
460460
}
@@ -588,7 +588,7 @@ func BenchmarkMarshal(b *testing.B) {
588588
}
589589
for _, bm := range moreBenchmarks {
590590
b.Run(bm.name, func(b *testing.B) {
591-
for i := 0; i < b.N; i++ {
591+
for b.Loop() {
592592
if _, err := Marshal(bm.value); err != nil {
593593
b.Fatal("Marshal:", err)
594594
}
@@ -634,7 +634,7 @@ func BenchmarkMarshalCanonical(b *testing.B) {
634634
name = "Go " + reflect.TypeOf(v).Kind().String() + " to CBOR " + bm.name
635635
}
636636
b.Run(name, func(b *testing.B) {
637-
for i := 0; i < b.N; i++ {
637+
for b.Loop() {
638638
if _, err := Marshal(v); err != nil {
639639
b.Fatal("Marshal:", err)
640640
}
@@ -647,7 +647,7 @@ func BenchmarkMarshalCanonical(b *testing.B) {
647647
}
648648
em, _ := EncOptions{Sort: SortCanonical}.EncMode()
649649
b.Run(name, func(b *testing.B) {
650-
for i := 0; i < b.N; i++ {
650+
for b.Loop() {
651651
if _, err := em.Marshal(v); err != nil {
652652
b.Fatal("Marshal:", err)
653653
}
@@ -666,8 +666,7 @@ func BenchmarkNewEncoderEncode(b *testing.B) {
666666
name = "Go " + reflect.TypeOf(v).Kind().String() + " to CBOR " + bm.name
667667
}
668668
b.Run(name, func(b *testing.B) {
669-
b.ResetTimer()
670-
for i := 0; i < b.N; i++ {
669+
for b.Loop() {
671670
encoder := NewEncoder(io.Discard)
672671
if err := encoder.Encode(v); err != nil {
673672
b.Fatal("Encode:", err)
@@ -689,8 +688,7 @@ func BenchmarkEncode(b *testing.B) {
689688
}
690689
b.Run(name, func(b *testing.B) {
691690
encoder := NewEncoder(io.Discard)
692-
b.ResetTimer()
693-
for i := 0; i < b.N; i++ {
691+
for b.Loop() {
694692
if err := encoder.Encode(v); err != nil {
695693
b.Fatal("Encode:", err)
696694
}
@@ -736,7 +734,7 @@ func BenchmarkUnmarshalCOSE(b *testing.B) {
736734
}
737735
for _, tc := range testCases {
738736
b.Run(tc.name, func(b *testing.B) {
739-
for i := 0; i < b.N; i++ {
737+
for b.Loop() {
740738
var v coseKey
741739
if err := Unmarshal(tc.data, &v); err != nil {
742740
b.Fatal("Unmarshal:", err)
@@ -771,7 +769,7 @@ func BenchmarkMarshalCOSE(b *testing.B) {
771769
b.Fatal("Unmarshal:", err)
772770
}
773771
b.Run(tc.name, func(b *testing.B) {
774-
for i := 0; i < b.N; i++ {
772+
for b.Loop() {
775773
if _, err := Marshal(v); err != nil {
776774
b.Fatal("Marshal:", err)
777775
}
@@ -1149,8 +1147,7 @@ func BenchmarkUnmarshalMapToStruct(b *testing.B) {
11491147

11501148
dst := reflect.New(reflect.TypeOf(in.into)).Interface()
11511149

1152-
b.ResetTimer()
1153-
for i := 0; i < b.N; i++ {
1150+
for b.Loop() {
11541151
if err := dm.Unmarshal(in.data, dst); !in.reject && err != nil {
11551152
b.Fatalf("unexpected error: %v", err)
11561153
} else if in.reject && err == nil {

decode_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5560,7 +5560,7 @@ func TestDecOptions(t *testing.T) {
55605560
JSONUnmarshalerTranscoder: stubTranscoder{},
55615561
}
55625562
ov := reflect.ValueOf(opts1)
5563-
for i := 0; i < ov.NumField(); i++ {
5563+
for i := range ov.NumField() {
55645564
fv := ov.Field(i)
55655565
if fv.IsZero() {
55665566
t.Errorf("options field %q is unset or set to the zero value for its type", ov.Type().Field(i).Name)

diagnose_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,8 +1160,7 @@ func BenchmarkDiagnose(b *testing.B) {
11601160
b.Fatal(err)
11611161
}
11621162

1163-
b.ResetTimer()
1164-
for i := 0; i < b.N; i++ {
1163+
for b.Loop() {
11651164
_, _ = dm.Diagnose(tc.input)
11661165
}
11671166
})

encode_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5623,7 +5623,7 @@ func TestEncOptions(t *testing.T) {
56235623
JSONMarshalerTranscoder: stubTranscoder{},
56245624
}
56255625
ov := reflect.ValueOf(opts1)
5626-
for i := 0; i < ov.NumField(); i++ {
5626+
for i := range ov.NumField() {
56275627
fv := ov.Field(i)
56285628
if fv.IsZero() {
56295629
fn := ov.Type().Field(i).Name

simplevalue_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
func TestUnmarshalSimpleValue(t *testing.T) {
1515
t.Run("0..23", func(t *testing.T) {
16-
for i := 0; i <= 23; i++ {
16+
for i := range 24 {
1717
data := []byte{byte(cborTypePrimitives) | byte(i)}
1818
want := SimpleValue(i)
1919

@@ -253,7 +253,7 @@ func testUnmarshalSimpleValue(t *testing.T, data []byte, want SimpleValue) {
253253

254254
func TestMarshalSimpleValue(t *testing.T) {
255255
t.Run("0..23", func(t *testing.T) {
256-
for i := 0; i <= 23; i++ {
256+
for i := range 24 {
257257
wantData := []byte{byte(cborTypePrimitives) | byte(i)}
258258
v := SimpleValue(i)
259259

stream_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func TestDecoderUnmarshalTypeError(t *testing.T) {
8282
var buf bytes.Buffer
8383
for range 5 {
8484
for _, tc := range unmarshalTestCases {
85-
for j := 0; j < len(tc.wrongTypes)*2; j++ {
85+
for range len(tc.wrongTypes) * 2 {
8686
buf.Write(tc.data)
8787
}
8888
}
@@ -187,7 +187,7 @@ func TestDecoderUnexpectedEOFError(t *testing.T) {
187187

188188
decoder := NewDecoder(tc.reader)
189189
bytesRead := 0
190-
for i := 0; i < len(unmarshalTestCases)-1; i++ {
190+
for i := range len(unmarshalTestCases) - 1 {
191191
tc := unmarshalTestCases[i]
192192
var v any
193193
if err := decoder.Decode(&v); err != nil {
@@ -251,7 +251,7 @@ func TestDecoderReadError(t *testing.T) {
251251
t.Run(tc.name, func(t *testing.T) {
252252
decoder := NewDecoder(tc.reader)
253253
bytesRead := 0
254-
for i := 0; i < len(unmarshalTestCases)-1; i++ {
254+
for i := range len(unmarshalTestCases) - 1 {
255255
tc := unmarshalTestCases[i]
256256
var v any
257257
if err := decoder.Decode(&v); err != nil {
@@ -513,7 +513,7 @@ func TestDecoderSkipUnexpectedEOFError(t *testing.T) {
513513
t.Run(tc.name, func(t *testing.T) {
514514
decoder := NewDecoder(tc.reader)
515515
bytesRead := 0
516-
for i := 0; i < len(unmarshalTestCases)-1; i++ {
516+
for i := range len(unmarshalTestCases) - 1 {
517517
tc := unmarshalTestCases[i]
518518
if err := decoder.Skip(); err != nil {
519519
t.Fatalf("Skip() returned error %v", err)
@@ -565,7 +565,7 @@ func TestDecoderSkipReadError(t *testing.T) {
565565
t.Run(tc.name, func(t *testing.T) {
566566
decoder := NewDecoder(tc.reader)
567567
bytesRead := 0
568-
for i := 0; i < len(unmarshalTestCases)-1; i++ {
568+
for i := range len(unmarshalTestCases) - 1 {
569569
tc := unmarshalTestCases[i]
570570
if err := decoder.Skip(); err != nil {
571571
t.Fatalf("Skip() returned error %v", err)

0 commit comments

Comments
 (0)