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 +}