Skip to content

Commit 0c80614

Browse files
demiurg906Space
authored and
Space
committed
[FE 1.0] Fix false-negative INVALID_IF_AS_EXPRESSION_WARNING and NO_ELSE_IN_WHEN_WARNING
^KT-51711 Fixed
1 parent 26b4c94 commit 0c80614

File tree

7 files changed

+63
-8
lines changed

7 files changed

+63
-8
lines changed

analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/frontend/cfg/src/org/jetbrains/kotlin/cfg/ControlFlowInformationProviderImpl.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -955,20 +955,23 @@ class ControlFlowInformationProviderImpl private constructor(
955955
for (element in instruction.owner.getValueElements(value)) {
956956
if (element !is KtIfExpression) continue
957957

958-
if (element.isUsedAsExpression(trace.bindingContext)) {
959-
val thenExpression = element.then
960-
val elseExpression = element.`else`
958+
val thenExpression = element.then
959+
val elseExpression = element.`else`
960+
val isEhxaustive = thenExpression != null && elseExpression != null
961961

962-
if (thenExpression == null || elseExpression == null) {
962+
if (element.isUsedAsExpression(trace.bindingContext)) {
963+
if (!isEhxaustive) {
963964
trace.report(INVALID_IF_AS_EXPRESSION.on(element.ifKeyword))
964965
} else {
965966
checkImplicitCastOnConditionalExpression(element)
966967
}
967968
} else if (!languageVersionSettings.supportsFeature(LanguageFeature.ProhibitNonExhaustiveIfInRhsOfElvis)) {
968-
val parent = element.deparenthesizedParent
969-
if (parent is KtBinaryExpression) {
970-
if (parent.operationToken === KtTokens.ELVIS) {
971-
trace.report(INVALID_IF_AS_EXPRESSION_WARNING.on(element.getIfKeyword()))
969+
if (!isEhxaustive) {
970+
val parent = element.deparenthesizedParent
971+
if (parent is KtBinaryExpression) {
972+
if (parent.operationToken === KtTokens.ELVIS) {
973+
trace.report(INVALID_IF_AS_EXPRESSION_WARNING.on(element.getIfKeyword()))
974+
}
972975
}
973976
}
974977
}
@@ -1067,6 +1070,7 @@ class ControlFlowInformationProviderImpl private constructor(
10671070
if (
10681071
!usedAsExpression &&
10691072
missingCases.isNotEmpty() &&
1073+
elseEntry == null &&
10701074
!languageVersionSettings.supportsFeature(LanguageFeature.ProhibitNonExhaustiveIfInRhsOfElvis)
10711075
) {
10721076
val parent = element.deparenthesizedParent
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// FIR_IDENTICAL
2+
// LANGUAGE: -ProhibitNonExhaustiveIfInRhsOfElvis
3+
// ISSUE: KT-51711
4+
// WITH_STDLIB
5+
6+
private fun fake(a: String?) {}
7+
8+
fun test_1(x: String?, y: String?) {
9+
while (true) {
10+
x ?: if (y == null) break else fake(y)
11+
}
12+
}
13+
14+
15+
fun test_2(x: String?, y: String?) {
16+
while (true) {
17+
x ?: when {
18+
true -> break
19+
else -> y?.filter { it.isLowerCase() }
20+
}
21+
}
22+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package
2+
3+
private fun fake(/*0*/ a: kotlin.String?): kotlin.Unit
4+
public fun test_1(/*0*/ x: kotlin.String?, /*1*/ y: kotlin.String?): kotlin.Unit
5+
public fun test_2(/*0*/ x: kotlin.String?, /*1*/ y: kotlin.String?): kotlin.Unit

compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)