From f8549a4fb84c31c9cbc36190044564f7db725181 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Wed, 4 Sep 2024 04:09:43 +0300 Subject: [PATCH] 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 {