Skip to content

Commit 70a1662

Browse files
authored
chore: lint fixes (#1074)
1 parent 18def95 commit 70a1662

25 files changed

+125
-118
lines changed

.github/workflows/lint.yml

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,5 @@ jobs:
1717
with:
1818
go-version: "1.23"
1919
- name: golangci-lint
20-
uses: golangci/golangci-lint-action@v6
21-
with:
22-
version: v1.55.2
20+
run: |
21+
make lint

.golangci.yml

+49-42
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1+
version: "2"
12
run:
23
tests: true
3-
# timeout for analysis, e.g. 30s, 5m, default is 1m
4-
timeout: 5m
5-
64
linters:
7-
disable-all: true
5+
default: none
86
enable:
97
- bodyclose
108
- dogsled
119
- errcheck
12-
- exportloopref
1310
- goconst
1411
- gocritic
15-
- gofumpt
1612
- gosec
17-
- gosimple
1813
- govet
1914
- ineffassign
2015
- misspell
@@ -23,44 +18,56 @@ linters:
2318
- prealloc
2419
- revive
2520
- staticcheck
26-
- stylecheck
27-
- typecheck
2821
- unconvert
2922
- unparam
3023
- unused
31-
32-
linters-settings:
33-
nolintlint:
34-
allow-leading-space: true
35-
require-explanation: false
36-
require-specific: true
37-
24+
settings:
25+
nolintlint:
26+
require-explanation: false
27+
require-specific: true
28+
exclusions:
29+
generated: lax
30+
presets:
31+
- comments
32+
- common-false-positives
33+
- legacy
34+
- std-error-handling
35+
rules:
36+
- linters:
37+
- gosec
38+
text: Use of weak random number generator
39+
- linters:
40+
- golint
41+
text: comment on exported var
42+
- linters:
43+
- golint
44+
text: don't use an underscore in package name
45+
- linters:
46+
- nolintlint
47+
text: should be written without leading space as
48+
- linters:
49+
- staticcheck
50+
text: 'ST1003:'
51+
- linters:
52+
- staticcheck
53+
text: 'ST1016:'
54+
- linters:
55+
- staticcheck
56+
path: migrations
57+
text: 'SA1019:'
58+
paths:
59+
- third_party$
60+
- builtin$
61+
- examples$
3862
issues:
39-
exclude-rules:
40-
- text: "Use of weak random number generator"
41-
linters:
42-
- gosec
43-
- text: "comment on exported var"
44-
linters:
45-
- golint
46-
- text: "don't use an underscore in package name"
47-
linters:
48-
- golint
49-
- text: "should be written without leading space as"
50-
linters:
51-
- nolintlint
52-
- text: "ST1003:"
53-
linters:
54-
- stylecheck
55-
# FIXME: Disabled until golangci-lint updates stylecheck with this fix:
56-
# https://github.com/dominikh/go-tools/issues/389
57-
- text: "ST1016:"
58-
linters:
59-
- stylecheck
60-
- path: "migrations"
61-
text: "SA1019:"
62-
linters:
63-
- staticcheck
64-
6563
max-issues-per-linter: 10000
6664
max-same-issues: 10000
65+
formatters:
66+
enable:
67+
- gofumpt
68+
exclusions:
69+
generated: lax
70+
paths:
71+
- third_party$
72+
- builtin$
73+
- examples$

Makefile

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ format:
3939

4040
# look into .golangci.yml for enabling / disabling linters
4141
golangci_lint_cmd=golangci-lint
42-
golangci_version=v1.59.1
42+
golangci_version=v2.0.2
4343

4444
lint:
4545
@echo "--> Running linter"
46-
@go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(golangci_version)
46+
@go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$(golangci_version)
4747
@$(golangci_lint_cmd) run --timeout=10m
4848

4949
lint-fix:
5050
@echo "--> Running linter"
51-
@go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(golangci_version)
52-
@$(golangci_lint_cmd) run --fix --out-format=tab --issues-exit-code=0
51+
@go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$(golangci_version)
52+
@$(golangci_lint_cmd) run --fix --issues-exit-code=0
5353

5454
# bench is the basic tests that shouldn't crash an aws instance
5555
bench:

basic_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ func TestRemove(_ *testing.T) {
244244

245245
// insert a bunch of random nodes
246246
keys := make([][]byte, size)
247-
l := int32(len(keys))
247+
l := int32(len(keys)) //nolint: gosec // used in testing
248248
for i := 0; i < size; i++ {
249249
key := iavlrand.RandBytes(keyLen)
250250
t1.Set(key, iavlrand.RandBytes(dataLen))

benchmarks/bench_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func runKnownQueriesFast(b *testing.B, t *iavl.MutableTree, keys [][]byte) {
7474
isFastCacheEnabled, err := t.IsFastCacheEnabled() // to ensure fast storage is enabled
7575
require.NoError(b, err)
7676
require.True(b, isFastCacheEnabled)
77-
l := int32(len(keys))
77+
l := int32(len(keys)) //nolint: gosec // used in testing
7878
for i := 0; i < b.N; i++ {
7979
q := keys[mrand.Int31n(l)]
8080
_, err := t.Get(q)
@@ -116,7 +116,7 @@ func runKnownQueriesSlow(b *testing.B, t *iavl.MutableTree, keys [][]byte) {
116116
require.NoError(b, err)
117117
require.False(b, isFastCacheEnabled)
118118
b.StartTimer()
119-
l := int32(len(keys))
119+
l := int32(len(keys)) //nolint: gosec // used in testing
120120
for i := 0; i < b.N; i++ {
121121
q := keys[mrand.Int31n(l)]
122122
index, value, err := itree.GetWithIndex(q)
@@ -173,7 +173,7 @@ func iterate(b *testing.B, itr corestore.Iterator, expectedSize int) {
173173
// }
174174

175175
func runUpdate(b *testing.B, t *iavl.MutableTree, dataLen, blockSize int, keys [][]byte) *iavl.MutableTree {
176-
l := int32(len(keys))
176+
l := int32(len(keys)) //nolint: gosec // used in testing
177177
for i := 1; i <= b.N; i++ {
178178
key := keys[mrand.Int31n(l)]
179179
_, err := t.Set(key, randBytes(dataLen))
@@ -202,7 +202,7 @@ func runUpdate(b *testing.B, t *iavl.MutableTree, dataLen, blockSize int, keys [
202202

203203
// runBlock measures time for an entire block, not just one tx
204204
func runBlock(b *testing.B, t *iavl.MutableTree, keyLen, dataLen, blockSize int, keys [][]byte) *iavl.MutableTree {
205-
l := int32(len(keys))
205+
l := int32(len(keys)) //nolint: gosec // used in testing
206206

207207
// XXX: This was adapted to work with VersionedTree but needs to be re-thought.
208208

benchmarks/cosmos-exim/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (s *Stats) AddNode(node *iavl.ExportNode) {
5050
if node.Height == 0 {
5151
s.leafNodes++
5252
}
53-
s.size += uint64(len(node.Key) + len(node.Value) + 8 + 1)
53+
s.size += uint64(len(node.Key) + len(node.Value) + 8 + 1) //nolint: gosec // used in testing
5454
}
5555

5656
func (s *Stats) String() string {

cmd/go.mod

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
module github.com/cosmos/iavl/cmd
22

3-
go 1.21
4-
toolchain go1.23.4
3+
go 1.23
4+
5+
toolchain go1.23.7
56

67
require (
7-
cosmossdk.io/core v1.0.0-alpha.6
8+
cosmossdk.io/core v1.0.0
89
cosmossdk.io/log v1.3.1
910
github.com/cosmos/iavl v1.2.0
1011
)

cmd/go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
cosmossdk.io/core v1.0.0-alpha.6 h1:5ukC4JcQKmemLQXcAgu/QoOvJI50hpBkIIg4ZT2EN8E=
2-
cosmossdk.io/core v1.0.0-alpha.6/go.mod h1:3u9cWq1FAVtiiCrDPpo4LhR+9V6k/ycSG4/Y/tREWCY=
1+
cosmossdk.io/core v1.0.0 h1:e7XBbISOytLBOXMVwpRPixThXqEkeLGlg8no/qpgS8U=
2+
cosmossdk.io/core v1.0.0/go.mod h1:mKIp3RkoEmtqdEdFHxHwWAULRe+79gfdOvmArrLDbDc=
33
cosmossdk.io/log v1.3.1 h1:UZx8nWIkfbbNEWusZqzAx3ZGvu54TZacWib3EzUYmGI=
44
cosmossdk.io/log v1.3.1/go.mod h1:2/dIomt8mKdk6vl3OWJcPk2be3pGOS8OQaLUM/3/tCM=
55
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=

cmd/iaviewer/main.go

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"bytes"
55
"crypto/sha256"
6-
"encoding/hex"
76
"errors"
87
"fmt"
98
"os"

compress.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (i *CompressImporter) Add(node *ExportNode) error {
9898
func deltaEncode(key, lastKey []byte) []byte {
9999
var sizeBuf [binary.MaxVarintLen64]byte
100100
shared := diffOffset(lastKey, key)
101-
n := binary.PutUvarint(sizeBuf[:], uint64(shared))
101+
n := binary.PutUvarint(sizeBuf[:], uint64(shared)) // nolint:gosec // shared will always be positive
102102
return append(sizeBuf[:n], key[shared:]...)
103103
}
104104

diff_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func genChangeSets(r *rand.Rand, n int) []*ChangeSet {
4646
start, count, step := r.Int63n(1000), r.Int63n(1000), r.Int63n(10)
4747
for i := start; i < start+count*step; i += step {
4848
value := make([]byte, 8)
49-
binary.LittleEndian.PutUint64(value, uint64(i))
49+
binary.LittleEndian.PutUint64(value, uint64(i)) // nolint:gosec // testing check
5050

5151
key := fmt.Sprintf("test-%d", i)
5252
items[key] = &KVPair{

internal/bytes/bytes.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ func (bz HexBytes) String() string {
5757
func (bz HexBytes) Format(s fmt.State, verb rune) {
5858
switch verb {
5959
case 'p':
60-
if _, err := s.Write([]byte(fmt.Sprintf("%p", bz))); err != nil {
60+
if _, err := fmt.Fprintf(s, "%p", bz); err != nil {
6161
panic(err)
6262
}
6363
default:
64-
if _, err := s.Write([]byte(fmt.Sprintf("%X", []byte(bz)))); err != nil {
64+
if _, err := fmt.Fprintf(s, "%X", []byte(bz)); err != nil {
6565
panic(err)
6666
}
6767
}

internal/encoding/encoding_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func TestDecodeBytes(t *testing.T) {
7575
require.Equal(t, varintBytes, n)
7676
} else {
7777
require.NoError(t, err)
78-
require.Equal(t, uint64(n), uint64(varintBytes)+tc.lengthPrefix)
78+
require.Equal(t, uint64(n), uint64(varintBytes)+tc.lengthPrefix) // nolint:gosec // testing check
7979
require.Equal(t, tc.bz[:tc.lengthPrefix], b)
8080
}
8181
})

internal/rand/random.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (r *Rand) init() {
4343
seed |= uint64(bz[i])
4444
seed <<= 8
4545
}
46-
r.reset(int64(seed))
46+
r.reset(int64(seed)) // nolint:gosec // only used in unsafe rand operations
4747
}
4848

4949
func (r *Rand) reset(seed int64) {
@@ -110,7 +110,7 @@ MAIN_LOOP:
110110
}
111111

112112
func (r *Rand) Uint16() uint16 {
113-
return uint16(r.Uint32() & (1<<16 - 1))
113+
return uint16(r.Uint32() & (1<<16 - 1)) // nolint:gosec // only used in unsafe rand operations
114114
}
115115

116116
func (r *Rand) Uint32() uint32 {
@@ -128,19 +128,19 @@ func (r *Rand) Uint() uint {
128128
r.Lock()
129129
i := r.rand.Int()
130130
r.Unlock()
131-
return uint(i)
131+
return uint(i) // nolint:gosec // only used in unsafe rand operations
132132
}
133133

134134
func (r *Rand) Int16() int16 {
135-
return int16(r.Uint32() & (1<<16 - 1))
135+
return int16(r.Uint32() & (1<<16 - 1)) // nolint:gosec // only used in unsafe rand operations
136136
}
137137

138138
func (r *Rand) Int32() int32 {
139-
return int32(r.Uint32())
139+
return int32(r.Uint32()) // nolint:gosec // only used in unsafe rand operations
140140
}
141141

142142
func (r *Rand) Int64() int64 {
143-
return int64(r.Uint64())
143+
return int64(r.Uint64()) // nolint:gosec // only used in unsafe rand operations
144144
}
145145

146146
func (r *Rand) Int() int {
@@ -193,7 +193,7 @@ func (r *Rand) Float64() float64 {
193193
}
194194

195195
func (r *Rand) Time() time.Time {
196-
return time.Unix(int64(r.Uint64()), 0)
196+
return time.Unix(int64(r.Uint64()), 0) // nolint:gosec // only used in unsafe rand operations
197197
}
198198

199199
// Bytes returns n random bytes generated from the internal

keyformat/key_format.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,13 @@ func scan(a interface{}, value []byte) {
155155
switch v := a.(type) {
156156
case *int64:
157157
// Negative values will be mapped correctly when read in as uint64 and then type converted
158-
*v = int64(binary.BigEndian.Uint64(value))
158+
*v = int64(binary.BigEndian.Uint64(value)) // nolint:gosec // the integer is always positive
159159
case *uint64:
160160
*v = binary.BigEndian.Uint64(value)
161161
case *uint32:
162162
*v = binary.BigEndian.Uint32(value)
163163
case *int32:
164-
*v = int32(binary.BigEndian.Uint32(value))
164+
*v = int32(binary.BigEndian.Uint32(value)) // nolint:gosec // the integer is always positive
165165
case *[]byte:
166166
*v = value
167167
case *big.Int:
@@ -176,16 +176,16 @@ func format(a interface{}) []byte {
176176
case uint64:
177177
return formatUint64(v)
178178
case int64:
179-
return formatUint64(uint64(v))
179+
return formatUint64(uint64(v)) // nolint:gosec // the integer version is always positive
180180
// Provide formatting from int,uint as a convenience to avoid casting arguments
181181
case uint:
182182
return formatUint64(uint64(v))
183183
case int:
184-
return formatUint64(uint64(v))
184+
return formatUint64(uint64(v)) // nolint:gosec // the integer version is always positive
185185
case uint32:
186186
return formatUint32(v)
187187
case int32:
188-
return formatUint32(uint32(v))
188+
return formatUint32(uint32(v)) // nolint:gosec // the integer version is always positive
189189
case []byte:
190190
return v
191191
default:

keyformat/key_format_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func TestOverflow(t *testing.T) {
116116
ao, bo := new(int64), new(int64)
117117
kf.Scan(key, ao, bo)
118118
assert.Equal(t, a, *ao)
119-
assert.Equal(t, int64(b), *bo)
119+
assert.Equal(t, int64(b), *bo) //nolint: gosec // used in testing
120120
}
121121

122122
func benchmarkKeyFormatBytes(b *testing.B, kf *KeyFormat, segments ...[]byte) {

keyformat/prefix_formatter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (f *FastPrefixFormatter) Scan(key []byte, a interface{}) {
2929
func (f *FastPrefixFormatter) KeyInt64(bz int64) []byte {
3030
key := make([]byte, 1+f.length)
3131
key[0] = f.prefix
32-
binary.BigEndian.PutUint64(key[1:], uint64(bz))
32+
binary.BigEndian.PutUint64(key[1:], uint64(bz)) // nolint:gosec // the integer version is always positive
3333
return key
3434
}
3535

0 commit comments

Comments
 (0)