Build fix: nullable exhaustiveness checker is now only a part of other exhaustiveness checkers
This commit is contained in:
@@ -72,12 +72,11 @@ private object NullMissingCase : WhenMissingCase {
|
|||||||
override val branchConditionText = "null"
|
override val branchConditionText = "null"
|
||||||
}
|
}
|
||||||
|
|
||||||
private object WhenOnNullableExhaustivenessChecker : WhenExhaustivenessChecker {
|
// It's not a regular exhaustiveness checker, invoke it only inside other checkers
|
||||||
override fun getMissingCases(expression: KtWhenExpression, context: BindingContext, subjectDescriptor: ClassDescriptor?, nullable: Boolean) =
|
private object WhenOnNullableExhaustivenessChecker /* : WhenExhaustivenessChecker*/ {
|
||||||
|
fun getMissingCases(expression: KtWhenExpression, context: BindingContext, subjectDescriptor: ClassDescriptor?, nullable: Boolean) =
|
||||||
if (nullable) getNullCaseIfMissing(expression, context) else listOf()
|
if (nullable) getNullCaseIfMissing(expression, context) else listOf()
|
||||||
|
|
||||||
override fun isApplicable(subjectType: KotlinType): Boolean = TypeUtils.isNullableType(subjectType)
|
|
||||||
|
|
||||||
private fun getNullCaseIfMissing(expression: KtWhenExpression, context: BindingContext): List<WhenMissingCase> {
|
private fun getNullCaseIfMissing(expression: KtWhenExpression, context: BindingContext): List<WhenMissingCase> {
|
||||||
for (entry in expression.entries) {
|
for (entry in expression.entries) {
|
||||||
for (condition in entry.conditions) {
|
for (condition in entry.conditions) {
|
||||||
@@ -121,7 +120,8 @@ private object WhenOnBooleanExhaustivenessChecker : WhenExhaustivenessChecker {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (if (!containsTrue) listOf(BooleanMissingCase(true)) else listOf()) +
|
return (if (!containsTrue) listOf(BooleanMissingCase(true)) else listOf()) +
|
||||||
(if (!containsFalse) listOf(BooleanMissingCase(false)) else listOf())
|
(if (!containsFalse) listOf(BooleanMissingCase(false)) else listOf()) +
|
||||||
|
WhenOnNullableExhaustivenessChecker.getMissingCases(expression, context, subjectDescriptor, nullable)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun isApplicable(subjectType: KotlinType): Boolean {
|
override fun isApplicable(subjectType: KotlinType): Boolean {
|
||||||
@@ -210,7 +210,8 @@ private object WhenOnEnumExhaustivenessChecker : WhenOnClassExhaustivenessChecke
|
|||||||
.filter { isEnumEntry(it) }
|
.filter { isEnumEntry(it) }
|
||||||
.filterIsInstance<ClassDescriptor>()
|
.filterIsInstance<ClassDescriptor>()
|
||||||
.toSet()
|
.toSet()
|
||||||
return getMissingClassCases(expression, entryDescriptors, context)
|
return getMissingClassCases(expression, entryDescriptors, context) +
|
||||||
|
WhenOnNullableExhaustivenessChecker.getMissingCases(expression, context, subjectDescriptor, nullable)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun isApplicable(subjectType: KotlinType): Boolean {
|
override fun isApplicable(subjectType: KotlinType): Boolean {
|
||||||
@@ -232,7 +233,8 @@ private object WhenOnSealedExhaustivenessChecker : WhenOnClassExhaustivenessChec
|
|||||||
val memberClassDescriptors = LinkedHashSet<ClassDescriptor>()
|
val memberClassDescriptors = LinkedHashSet<ClassDescriptor>()
|
||||||
collectNestedSubclasses(subjectDescriptor!!, subjectDescriptor, memberClassDescriptors)
|
collectNestedSubclasses(subjectDescriptor!!, subjectDescriptor, memberClassDescriptors)
|
||||||
// When on a sealed class without derived members is considered non-exhaustive (see test WhenOnEmptySealed)
|
// When on a sealed class without derived members is considered non-exhaustive (see test WhenOnEmptySealed)
|
||||||
return getMissingClassCases(expression, memberClassDescriptors, context)
|
return getMissingClassCases(expression, memberClassDescriptors, context) +
|
||||||
|
WhenOnNullableExhaustivenessChecker.getMissingCases(expression, context, subjectDescriptor, nullable)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun isApplicable(subjectType: KotlinType): Boolean {
|
override fun isApplicable(subjectType: KotlinType): Boolean {
|
||||||
@@ -259,8 +261,7 @@ object WhenChecker {
|
|||||||
|
|
||||||
private val exhaustivenessCheckers = listOf(WhenOnBooleanExhaustivenessChecker,
|
private val exhaustivenessCheckers = listOf(WhenOnBooleanExhaustivenessChecker,
|
||||||
WhenOnEnumExhaustivenessChecker,
|
WhenOnEnumExhaustivenessChecker,
|
||||||
WhenOnSealedExhaustivenessChecker,
|
WhenOnSealedExhaustivenessChecker)
|
||||||
WhenOnNullableExhaustivenessChecker)
|
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun getNecessaryCases(expression: KtWhenExpression, context: BindingContext) =
|
fun getNecessaryCases(expression: KtWhenExpression, context: BindingContext) =
|
||||||
|
|||||||
+10
@@ -6,6 +6,11 @@ fun nonExhaustiveBoolean(b: Boolean) = <error descr="[NO_ELSE_IN_WHEN] when expr
|
|||||||
false -> 0
|
false -> 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun nonExhaustiveNullableBoolean(b: Boolean?) = <error descr="[NO_ELSE_IN_WHEN] when expression must be exhaustive, add necessary 'null' branch or 'else' branch instead">when</error>(b) {
|
||||||
|
false -> 0
|
||||||
|
true -> 1
|
||||||
|
}
|
||||||
|
|
||||||
enum class Color {
|
enum class Color {
|
||||||
RED,
|
RED,
|
||||||
GREEN,
|
GREEN,
|
||||||
@@ -48,6 +53,11 @@ fun nonExhaustiveSealed(v: Variant) = <error descr="[NO_ELSE_IN_WHEN] when expre
|
|||||||
Variant.Singleton -> false
|
Variant.Singleton -> false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun nonExhaustiveNullableSealed(v: Variant?) = <error descr="[NO_ELSE_IN_WHEN] when expression must be exhaustive, add necessary 'Another', 'null' branches or 'else' branch instead">when</error>(v) {
|
||||||
|
Variant.Singleton -> false
|
||||||
|
is Variant.Something -> true
|
||||||
|
}
|
||||||
|
|
||||||
sealed class Empty
|
sealed class Empty
|
||||||
|
|
||||||
fun nonExhaustiveEmpty(e: Empty) = <error descr="[NO_ELSE_IN_WHEN] when expression must be exhaustive, add necessary 'else' branch">when</error>(<warning>e</warning>) {}
|
fun nonExhaustiveEmpty(e: Empty) = <error descr="[NO_ELSE_IN_WHEN] when expression must be exhaustive, add necessary 'else' branch">when</error>(<warning>e</warning>) {}
|
||||||
|
|||||||
Reference in New Issue
Block a user