diff --git a/compiler/frontend/src/org/jetbrains/kotlin/cfg/UnreachableCode.kt b/compiler/frontend/src/org/jetbrains/kotlin/cfg/UnreachableCode.kt index b063e4923bb..fbb7b6394cf 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/cfg/UnreachableCode.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/cfg/UnreachableCode.kt @@ -42,7 +42,12 @@ class UnreachableCodeImpl( override fun getUnreachableTextRanges(element: KtElement): List { return if (element.hasChildrenInSet(reachableElements)) { - element.getLeavesOrReachableChildren().removeReachableElementsWithMeaninglessSiblings().mergeAdjacentTextRanges() + with (element.getLeavesOrReachableChildren().removeReachableElementsWithMeaninglessSiblings().mergeAdjacentTextRanges()) { + if (isNotEmpty()) this + // Specific case like condition in when: + // element is dead but its only child is alive and has the same text range + else listOf(element.textRange.endOffset.let { TextRange(it, it) }) + } } else { listOf(element.textRange!!) diff --git a/compiler/testData/cli/jvm/returnAsWhenKey.args b/compiler/testData/cli/jvm/returnAsWhenKey.args new file mode 100644 index 00000000000..c9c6c56982d --- /dev/null +++ b/compiler/testData/cli/jvm/returnAsWhenKey.args @@ -0,0 +1,3 @@ +$TESTDATA_DIR$/returnAsWhenKey.kt +-d +$TEMP_DIR$ diff --git a/compiler/testData/cli/jvm/returnAsWhenKey.kt b/compiler/testData/cli/jvm/returnAsWhenKey.kt new file mode 100644 index 00000000000..f3ba32ba181 --- /dev/null +++ b/compiler/testData/cli/jvm/returnAsWhenKey.kt @@ -0,0 +1,7 @@ +// KT-14158: NoSuchElementException while compiling... + +fun foo(): Int { + when (true) { + return 0 -> return 1 + } +} \ No newline at end of file diff --git a/compiler/testData/cli/jvm/returnAsWhenKey.out b/compiler/testData/cli/jvm/returnAsWhenKey.out new file mode 100644 index 00000000000..eb51fd96dcd --- /dev/null +++ b/compiler/testData/cli/jvm/returnAsWhenKey.out @@ -0,0 +1,7 @@ +compiler/testData/cli/jvm/returnAsWhenKey.kt:5:17: warning: unreachable code + return 0 -> return 1 + ^ +compiler/testData/cli/jvm/returnAsWhenKey.kt:5:21: warning: unreachable code + return 0 -> return 1 + ^ +OK \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/controlStructures/kt786.kt b/compiler/testData/diagnostics/tests/controlStructures/kt786.kt index 0ef7cfe564e..5d8ddba83e9 100644 --- a/compiler/testData/diagnostics/tests/controlStructures/kt786.kt +++ b/compiler/testData/diagnostics/tests/controlStructures/kt786.kt @@ -16,7 +16,7 @@ fun fff(): Int { var d = 3 when(d) { 4 -> 21 - return 2 -> return 47 + return 2 -> return 47 bar() -> 45 444 -> true } diff --git a/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java index 0b8765977ba..9e522fd6b15 100644 --- a/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java @@ -253,6 +253,12 @@ public class CliTestGenerated extends AbstractCliTest { doJvmTest(fileName); } + @TestMetadata("returnAsWhenKey.args") + public void testReturnAsWhenKey() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/jvm/returnAsWhenKey.args"); + doJvmTest(fileName); + } + @TestMetadata("sanitized-name.clash.args") public void testSanitized_name_clash() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/jvm/sanitized-name.clash.args");