diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/sequence/IntroduceIndexMatcher.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/sequence/IntroduceIndexMatcher.kt index 31b6151e731..dd46450bea7 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/sequence/IntroduceIndexMatcher.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/sequence/IntroduceIndexMatcher.kt @@ -93,6 +93,8 @@ object IntroduceIndexMatcher : TransformationMatcher { val visited = HashSet() return traverseFollowingInstructions(from, visited) { instruction -> val nextInstructionScope = instruction.blockScope.block + // we should either reach the target instruction or exit the outer loop on every branch + // (if we won't do this on some branch we will finally exit the inner loop and return false from traverseFollowingInstructions) when { instruction == to -> TraverseInstructionResult.SKIP !outerLoop.isAncestor(nextInstructionScope, strict = false) -> TraverseInstructionResult.SKIP // we are out of the outer loop - it's ok