[AA] KtExpressionInfoProvider.getMissingCases: fix no subject case

return missing else branch (even if it's present, see kdoc)

^ KT-62875 fixed
This commit is contained in:
Anna Kozlova
2023-10-24 22:09:45 +02:00
committed by Space Team
parent 19104f73bf
commit e79bc5bb59
12 changed files with 103 additions and 1 deletions
@@ -58,6 +58,24 @@ public class Fe10IdeNormalAnalysisSourceModuleWhenMissingCasesTestGenerated exte
runTest("analysis/analysis-api/testData/components/expressionInfoProvider/whenMissingCases/boolean_empty.kt");
}
@Test
@TestMetadata("boolean_noSubject.kt")
public void testBoolean_noSubject() throws Exception {
runTest("analysis/analysis-api/testData/components/expressionInfoProvider/whenMissingCases/boolean_noSubject.kt");
}
@Test
@TestMetadata("boolean_noSubjectIncorrectCode.kt")
public void testBoolean_noSubjectIncorrectCode() throws Exception {
runTest("analysis/analysis-api/testData/components/expressionInfoProvider/whenMissingCases/boolean_noSubjectIncorrectCode.kt");
}
@Test
@TestMetadata("boolean_noSubject_else.kt")
public void testBoolean_noSubject_else() throws Exception {
runTest("analysis/analysis-api/testData/components/expressionInfoProvider/whenMissingCases/boolean_noSubject_else.kt");
}
@Test
@TestMetadata("boolean_partial.kt")
public void testBoolean_partial() throws Exception {
@@ -58,6 +58,24 @@ public class FirIdeDependentAnalysisSourceModuleWhenMissingCasesTestGenerated ex
runTest("analysis/analysis-api/testData/components/expressionInfoProvider/whenMissingCases/boolean_empty.kt");
}
@Test
@TestMetadata("boolean_noSubject.kt")
public void testBoolean_noSubject() throws Exception {
runTest("analysis/analysis-api/testData/components/expressionInfoProvider/whenMissingCases/boolean_noSubject.kt");
}
@Test
@TestMetadata("boolean_noSubjectIncorrectCode.kt")
public void testBoolean_noSubjectIncorrectCode() throws Exception {
runTest("analysis/analysis-api/testData/components/expressionInfoProvider/whenMissingCases/boolean_noSubjectIncorrectCode.kt");
}
@Test
@TestMetadata("boolean_noSubject_else.kt")
public void testBoolean_noSubject_else() throws Exception {
runTest("analysis/analysis-api/testData/components/expressionInfoProvider/whenMissingCases/boolean_noSubject_else.kt");
}
@Test
@TestMetadata("boolean_partial.kt")
public void testBoolean_partial() throws Exception {
@@ -58,6 +58,24 @@ public class FirIdeNormalAnalysisSourceModuleWhenMissingCasesTestGenerated exten
runTest("analysis/analysis-api/testData/components/expressionInfoProvider/whenMissingCases/boolean_empty.kt");
}
@Test
@TestMetadata("boolean_noSubject.kt")
public void testBoolean_noSubject() throws Exception {
runTest("analysis/analysis-api/testData/components/expressionInfoProvider/whenMissingCases/boolean_noSubject.kt");
}
@Test
@TestMetadata("boolean_noSubjectIncorrectCode.kt")
public void testBoolean_noSubjectIncorrectCode() throws Exception {
runTest("analysis/analysis-api/testData/components/expressionInfoProvider/whenMissingCases/boolean_noSubjectIncorrectCode.kt");
}
@Test
@TestMetadata("boolean_noSubject_else.kt")
public void testBoolean_noSubject_else() throws Exception {
runTest("analysis/analysis-api/testData/components/expressionInfoProvider/whenMissingCases/boolean_noSubject_else.kt");
}
@Test
@TestMetadata("boolean_partial.kt")
public void testBoolean_partial() throws Exception {
@@ -58,6 +58,24 @@ public class FirStandaloneNormalAnalysisSourceModuleWhenMissingCasesTestGenerate
runTest("analysis/analysis-api/testData/components/expressionInfoProvider/whenMissingCases/boolean_empty.kt");
}
@Test
@TestMetadata("boolean_noSubject.kt")
public void testBoolean_noSubject() throws Exception {
runTest("analysis/analysis-api/testData/components/expressionInfoProvider/whenMissingCases/boolean_noSubject.kt");
}
@Test
@TestMetadata("boolean_noSubjectIncorrectCode.kt")
public void testBoolean_noSubjectIncorrectCode() throws Exception {
runTest("analysis/analysis-api/testData/components/expressionInfoProvider/whenMissingCases/boolean_noSubjectIncorrectCode.kt");
}
@Test
@TestMetadata("boolean_noSubject_else.kt")
public void testBoolean_noSubject_else() throws Exception {
runTest("analysis/analysis-api/testData/components/expressionInfoProvider/whenMissingCases/boolean_noSubject_else.kt");
}
@Test
@TestMetadata("boolean_partial.kt")
public void testBoolean_partial() throws Exception {
@@ -36,6 +36,15 @@ public interface KtExpressionInfoProviderMixIn : KtAnalysisSessionMixIn {
* else -> 3
* }
*
* If when-expression has no subject, then else-branch would be reported as missing even if it is explicitly present:
*
* fun test() {
* when {
* true -> {}
* else -> {}
* }
* }
*
* Note that this function returns the same missing cases regardless of the existence of the else branch.
* If you have to assume that it does not have the missing cases when it has an else branch,
* you need a separate check whether it has an else branch or not.
@@ -0,0 +1,5 @@
fun test() {
<caret>when {
true -> {}
}
}
@@ -0,0 +1,5 @@
fun test() {
<caret>when() {
true -> {}
}
}
@@ -0,0 +1,6 @@
fun test() {
<caret>when {
true -> {}
else -> { }
}
}
@@ -19,6 +19,7 @@ import org.jetbrains.kotlin.fir.declarations.utils.isExpect
import org.jetbrains.kotlin.fir.declarations.utils.modality
import org.jetbrains.kotlin.fir.enumWhenTracker
import org.jetbrains.kotlin.fir.expressions.*
import org.jetbrains.kotlin.fir.expressions.ExhaustivenessStatus.NotExhaustive
import org.jetbrains.kotlin.fir.expressions.impl.FirElseIfTrueCondition
import org.jetbrains.kotlin.fir.reportEnumUsageInWhen
import org.jetbrains.kotlin.fir.resolve.BodyResolveComponents
@@ -43,7 +44,8 @@ class FirWhenExhaustivenessTransformer(private val bodyResolveComponents: BodyRe
)
fun computeAllMissingCases(session: FirSession, whenExpression: FirWhenExpression): List<WhenMissingCase> {
val subjectType = getSubjectType(session, whenExpression) ?: return emptyList()
val subjectType =
getSubjectType(session, whenExpression) ?: return NotExhaustive.NO_ELSE_BRANCH.reasons
return buildList {
for (type in subjectType.unwrapIntersectionType()) {
val checkers = getCheckers(type, session)