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