From 3a3e787b32ea1630cd5d4e0311b445e9701aea16 Mon Sep 17 00:00:00 2001 From: Olga Klisho Date: Wed, 25 Feb 2026 12:40:06 +0000 Subject: [PATCH] [kotlin] KTIJ-37692 fix open range in ReplaceManualRangeWithIndicesCallsInspection (cherry picked from commit 976cf795ab226678e66d45b1164309cf99971cde) IJ-CR-193475 GitOrigin-RevId: 1549148f078ee0374c7af4c07a8d73c154618480 --- .../tests/K2LocalInspectionTestGenerated.java | 10 ++++++++++ .../ReplaceManualRangeWithIndicesCallsInspection.kt | 6 +++++- .../openRangeLastIndex.kt | 6 ++++++ .../untilLastIndex.kt | 5 +++++ 4 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceManualRangeWithIndicesCalls/openRangeLastIndex.kt create mode 100644 plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceManualRangeWithIndicesCalls/untilLastIndex.kt diff --git a/plugins/kotlin/code-insight/inspections-k2/tests/test/org/jetbrains/kotlin/idea/k2/inspections/tests/K2LocalInspectionTestGenerated.java b/plugins/kotlin/code-insight/inspections-k2/tests/test/org/jetbrains/kotlin/idea/k2/inspections/tests/K2LocalInspectionTestGenerated.java index c494040d4ebd..92c01f5dd994 100644 --- a/plugins/kotlin/code-insight/inspections-k2/tests/test/org/jetbrains/kotlin/idea/k2/inspections/tests/K2LocalInspectionTestGenerated.java +++ b/plugins/kotlin/code-insight/inspections-k2/tests/test/org/jetbrains/kotlin/idea/k2/inspections/tests/K2LocalInspectionTestGenerated.java @@ -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"); diff --git a/plugins/kotlin/code-insight/inspections-shared/src/org/jetbrains/kotlin/idea/codeInsight/inspections/shared/ReplaceManualRangeWithIndicesCallsInspection.kt b/plugins/kotlin/code-insight/inspections-shared/src/org/jetbrains/kotlin/idea/codeInsight/inspections/shared/ReplaceManualRangeWithIndicesCallsInspection.kt index 85f7c8f53e5b..1168dc66f514 100644 --- a/plugins/kotlin/code-insight/inspections-shared/src/org/jetbrains/kotlin/idea/codeInsight/inspections/shared/ReplaceManualRangeWithIndicesCallsInspection.kt +++ b/plugins/kotlin/code-insight/inspections-shared/src/org/jetbrains/kotlin/idea/codeInsight/inspections/shared/ReplaceManualRangeWithIndicesCallsInspection.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.. if (receiverType.isArrayOrPrimitiveArray || receiverType.isSubtypeOf(StandardClassIds.Collection)) target else null "length" -> if (receiverType.isSubtypeOf(StandardClassIds.CharSequence)) target else null diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceManualRangeWithIndicesCalls/openRangeLastIndex.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceManualRangeWithIndicesCalls/openRangeLastIndex.kt new file mode 100644 index 000000000000..e5da3e0d4a03 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceManualRangeWithIndicesCalls/openRangeLastIndex.kt @@ -0,0 +1,6 @@ +// WITH_STDLIB +// LANGUAGE_VERSION: 1.9 +// PROBLEM: none +fun test(list: List) { + val x = 42 in 0..) { + val x = 42 in 0 until list.lastIndex +}