[kotlin] KTIJ-37692 fix open range in ReplaceManualRangeWithIndicesCallsInspection

(cherry picked from commit 976cf795ab226678e66d45b1164309cf99971cde)

IJ-CR-193475

GitOrigin-RevId: 1549148f078ee0374c7af4c07a8d73c154618480
This commit is contained in:
Olga Klisho
2026-02-26 13:05:32 +00:00
committed by intellij-monorepo-bot
parent a3a693fb75
commit 3a3e787b32
4 changed files with 26 additions and 1 deletions
@@ -9851,6 +9851,11 @@ public abstract class K2LocalInspectionTestGenerated extends AbstractK2LocalInsp
runTest("../../../idea/tests/testData/inspectionsLocal/replaceManualRangeWithIndicesCalls/notUsedAsIndex.kt");
}
@TestMetadata("openRangeLastIndex.kt")
public void testOpenRangeLastIndex() throws Exception {
runTest("../../../idea/tests/testData/inspectionsLocal/replaceManualRangeWithIndicesCalls/openRangeLastIndex.kt");
}
@TestMetadata("qualifiedReceiverDifferentCollection.kt")
public void testQualifiedReceiverDifferentCollection() throws Exception {
runTest("../../../idea/tests/testData/inspectionsLocal/replaceManualRangeWithIndicesCalls/qualifiedReceiverDifferentCollection.kt");
@@ -9921,6 +9926,11 @@ public abstract class K2LocalInspectionTestGenerated extends AbstractK2LocalInsp
runTest("../../../idea/tests/testData/inspectionsLocal/replaceManualRangeWithIndicesCalls/typeMismatch.kt");
}
@TestMetadata("untilLastIndex.kt")
public void testUntilLastIndex() throws Exception {
runTest("../../../idea/tests/testData/inspectionsLocal/replaceManualRangeWithIndicesCalls/untilLastIndex.kt");
}
@TestMetadata("usedForOtherArray.kt")
public void testUsedForOtherArray() throws Exception {
runTest("../../../idea/tests/testData/inspectionsLocal/replaceManualRangeWithIndicesCalls/usedForOtherArray.kt");
@@ -201,7 +201,11 @@ class ReplaceManualRangeWithIndicesCallsInspection : KotlinApplicableInspectionB
val target = extractTargetExpression(type, expression) ?: return null
val selector = (target as? KtDotQualifiedExpression)?.selectorExpression ?: target
val receiverType = resolveReceiverType(target) ?: return null
// lastIndex is only valid for inclusive ranges (RANGE_TO)
// For open ranges (UNTIL, RANGE_UNTIL), 0..<lastIndex excludes the last element
if (selector.text == "lastIndex" && type != RANGE_TO) return null
return when (selector.text) {
"size", "lastIndex" -> if (receiverType.isArrayOrPrimitiveArray || receiverType.isSubtypeOf(StandardClassIds.Collection)) target else null
"length" -> if (receiverType.isSubtypeOf(StandardClassIds.CharSequence)) target else null
@@ -0,0 +1,6 @@
// WITH_STDLIB
// LANGUAGE_VERSION: 1.9
// PROBLEM: none
fun test(list: List<String>) {
val x = 42 in 0<caret>..<list.lastIndex
}
@@ -0,0 +1,5 @@
// WITH_STDLIB
// PROBLEM: none
fun test(list: List<String>) {
val x = 42 in 0 <caret>until list.lastIndex
}