Skip to content

Commit 320d33b

Browse files
committed
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 <carpawell@nspcc.ru>
1 parent e3700ea commit 320d33b

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

pkg/core/mpt/helpers.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,13 @@ func lcp(a, b []byte) []byte {
1313
return lcp(b, a)
1414
}
1515

16-
var i int
17-
//nolint:intrange // if slices are the same (or one is a prefix for another
18-
// one), `range` loops does not assign the latest index to the `i` var, and
19-
// the func loses the latest element
20-
for i = 0; i < len(b); i++ {
16+
for i := range b {
2117
if a[i] != b[i] {
22-
break
18+
return b[:i]
2319
}
2420
}
2521

26-
return a[:i]
22+
return b
2723
}
2824

2925
func lcpMany(kv []keyValue) []byte {

0 commit comments

Comments
 (0)