From 32f91dd72674ec996146829f0dcfe8f4f0d4c24b Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 3 Sep 2024 23:12:44 +0300 Subject: [PATCH 1/2] .golangci.yml: add `intrange` linter It checks that go 1.22's range-over-numbers feature is not skipped. Also, fix some warnings it found. Signed-off-by: Pavel Karpy --- .golangci.yml | 1 + internal/testchain/address.go | 2 +- pkg/core/blockchain_core_test.go | 4 ++-- pkg/core/mpt/helpers.go | 3 +++ pkg/core/native/native_test/neo_test.go | 2 +- pkg/vm/vm.go | 2 +- 6 files changed, 9 insertions(+), 5 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 5169c5a42c..cd3800d31d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -51,6 +51,7 @@ linters: - decorder - durationcheck - errorlint + - intrange - copyloopvar - gofmt - misspell diff --git a/internal/testchain/address.go b/internal/testchain/address.go index eabdc41c1b..2de4ea805e 100644 --- a/internal/testchain/address.go +++ b/internal/testchain/address.go @@ -142,7 +142,7 @@ func Sign(h hash.Hashable) []byte { // SignCommittee signs data by a majority of committee members. func SignCommittee(h hash.Hashable) []byte { buf := io.NewBufBinWriter() - for i := 0; i < CommitteeSize()/2+1; i++ { + for i := range CommitteeSize()/2 + 1 { pKey := PrivateKey(i) sig := pKey.SignHashable(uint32(Network()), h) if len(sig) != 64 { diff --git a/pkg/core/blockchain_core_test.go b/pkg/core/blockchain_core_test.go index 4db49bfff0..b2a10f21a0 100644 --- a/pkg/core/blockchain_core_test.go +++ b/pkg/core/blockchain_core_test.go @@ -116,7 +116,7 @@ func TestRemoveOldTransfers(t *testing.T) { require.NoError(t, err) require.NotEqual(t, 0, len(log.Raw)) - for i := uint32(0); i < 3; i++ { + for i := range uint32(3) { log, err = bc.dao.GetTokenTransferLog(acc2, newer, i, false) require.NoError(t, err) require.NotEqual(t, 0, len(log.Raw)) @@ -130,7 +130,7 @@ func TestRemoveOldTransfers(t *testing.T) { require.NoError(t, err) require.NotEqual(t, 0, len(log.Raw)) - for i := uint32(0); i < 2; i++ { + for i := range uint32(2) { log, err = bc.dao.GetTokenTransferLog(acc3, newer, i, true) require.NoError(t, err) require.NotEqual(t, 0, len(log.Raw)) diff --git a/pkg/core/mpt/helpers.go b/pkg/core/mpt/helpers.go index ad31a664f3..db560d6272 100644 --- a/pkg/core/mpt/helpers.go +++ b/pkg/core/mpt/helpers.go @@ -14,6 +14,9 @@ func lcp(a, b []byte) []byte { } var i int + //nolint:intrange // if slices are the same (or one is a prefix for another + // one), `range` loop does not assign the latest index to the `i` var, and + // the func loses the latest element for i = 0; i < len(b); i++ { if a[i] != b[i] { break diff --git a/pkg/core/native/native_test/neo_test.go b/pkg/core/native/native_test/neo_test.go index 1880c15565..ac02586422 100644 --- a/pkg/core/native/native_test/neo_test.go +++ b/pkg/core/native/native_test/neo_test.go @@ -821,7 +821,7 @@ func TestNEO_GetCandidates(t *testing.T) { for i := range len(expected) + 1 { w := io.NewBufBinWriter() emit.AppCall(w.BinWriter, neoCommitteeInvoker.Hash, "getAllCandidates", callflag.All) - for j := 0; j < i+1; j++ { + for range i + 1 { emit.Opcodes(w.BinWriter, opcode.DUP) emit.Syscall(w.BinWriter, interopnames.SystemIteratorNext) emit.Opcodes(w.BinWriter, opcode.DROP) // drop the value returned from Next. diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index 5c9b03b7b2..f9eac2e922 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -1860,7 +1860,7 @@ func (v *VM) ContractHasTryBlock() bool { if ictx.sc != topctx.sc { return false // Different contract -> no one cares. } - for j := 0; j < ictx.tryStack.Len(); j++ { + for j := range ictx.tryStack.Len() { eCtx := ictx.tryStack.Peek(j).Value().(*exceptionHandlingContext) if eCtx.State == eTry { return true From f8549a4fb84c31c9cbc36190044564f7db725181 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Wed, 4 Sep 2024 04:09:43 +0300 Subject: [PATCH 2/2] mpt: refactor `lcp` to be possible to use `range` It took some time to understand why changing a regular `for` to a `range` one leads to behavior changes; let it be more clear and explicit. Also, a correct code is always better than a correct code with `nolint`. Signed-off-by: Pavel Karpy --- pkg/core/mpt/helpers.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/pkg/core/mpt/helpers.go b/pkg/core/mpt/helpers.go index db560d6272..340bfa4445 100644 --- a/pkg/core/mpt/helpers.go +++ b/pkg/core/mpt/helpers.go @@ -13,17 +13,13 @@ func lcp(a, b []byte) []byte { return lcp(b, a) } - var i int - //nolint:intrange // if slices are the same (or one is a prefix for another - // one), `range` loop does not assign the latest index to the `i` var, and - // the func loses the latest element - for i = 0; i < len(b); i++ { + for i := range b { if a[i] != b[i] { - break + return b[:i] } } - return a[:i] + return b } func lcpMany(kv []keyValue) []byte {