Skip to content

Commit

Permalink
[FE 1.0] Fix false-negative INVALID_IF_AS_EXPRESSION_WARNING and NO_E…
Browse files Browse the repository at this point in the history
…LSE_IN_WHEN_WARNING

^KT-51711 Fixed
  • Loading branch information
demiurg906 authored and Space committed Apr 14, 2022
1 parent 26b4c94 commit 0c80614
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 8 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -955,20 +955,23 @@ class ControlFlowInformationProviderImpl private constructor(
for (element in instruction.owner.getValueElements(value)) {
if (element !is KtIfExpression) continue

if (element.isUsedAsExpression(trace.bindingContext)) {
val thenExpression = element.then
val elseExpression = element.`else`
val thenExpression = element.then
val elseExpression = element.`else`
val isEhxaustive = thenExpression != null && elseExpression != null

if (thenExpression == null || elseExpression == null) {
if (element.isUsedAsExpression(trace.bindingContext)) {
if (!isEhxaustive) {
trace.report(INVALID_IF_AS_EXPRESSION.on(element.ifKeyword))
} else {
checkImplicitCastOnConditionalExpression(element)
}
} else if (!languageVersionSettings.supportsFeature(LanguageFeature.ProhibitNonExhaustiveIfInRhsOfElvis)) {
val parent = element.deparenthesizedParent
if (parent is KtBinaryExpression) {
if (parent.operationToken === KtTokens.ELVIS) {
trace.report(INVALID_IF_AS_EXPRESSION_WARNING.on(element.getIfKeyword()))
if (!isEhxaustive) {
val parent = element.deparenthesizedParent
if (parent is KtBinaryExpression) {
if (parent.operationToken === KtTokens.ELVIS) {
trace.report(INVALID_IF_AS_EXPRESSION_WARNING.on(element.getIfKeyword()))
}
}
}
}
Expand Down Expand Up @@ -1067,6 +1070,7 @@ class ControlFlowInformationProviderImpl private constructor(
if (
!usedAsExpression &&
missingCases.isNotEmpty() &&
elseEntry == null &&
!languageVersionSettings.supportsFeature(LanguageFeature.ProhibitNonExhaustiveIfInRhsOfElvis)
) {
val parent = element.deparenthesizedParent
Expand Down
22 changes: 22 additions & 0 deletions compiler/testData/diagnostics/tests/controlStructures/kt51711.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// FIR_IDENTICAL
// LANGUAGE: -ProhibitNonExhaustiveIfInRhsOfElvis
// ISSUE: KT-51711
// WITH_STDLIB

private fun fake(a: String?) {}

fun test_1(x: String?, y: String?) {
while (true) {
x ?: if (y == null) break else fake(y)
}
}


fun test_2(x: String?, y: String?) {
while (true) {
x ?: when {
true -> break
else -> y?.filter { it.isLowerCase() }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package

private fun fake(/*0*/ a: kotlin.String?): kotlin.Unit
public fun test_1(/*0*/ x: kotlin.String?, /*1*/ y: kotlin.String?): kotlin.Unit
public fun test_2(/*0*/ x: kotlin.String?, /*1*/ y: kotlin.String?): kotlin.Unit

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0c80614

Please sign in to comment.