Skip to content

Commit

Permalink
Refactor MissingWhenCase (#3207)
Browse files Browse the repository at this point in the history
Puts the super.visit on top.
  • Loading branch information
schalkms authored and arturbosch committed Dec 14, 2020
1 parent dd6d4ab commit 81d5dde
Showing 1 changed file with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,24 @@ class MissingWhenCase(config: Config = Config.empty) : Rule(config) {

private val allowElseExpression = valueOrDefault(ALLOW_ELSE_EXPRESSION, true)

@Suppress("ReturnCount")
override fun visitWhenExpression(expression: KtWhenExpression) {
super.visitWhenExpression(expression)
if (bindingContext == BindingContext.EMPTY) return
if (allowElseExpression && expression.elseExpression != null) return
checkMissingWhenExpression(expression)
}

private fun checkMissingWhenExpression(expression: KtWhenExpression) {
if (expression.isUsedAsExpression(bindingContext)) return
val subjectExpression = expression.subjectExpression ?: return

val subjectType = subjectExpression.getType(bindingContext)
val enumClassDescriptor = WhenChecker.getClassDescriptorOfTypeIfEnum(subjectType)
val sealedClassDescriptor = WhenChecker.getClassDescriptorOfTypeIfSealed(subjectType)
if (enumClassDescriptor != null || sealedClassDescriptor != null) {
val missingCases = WhenChecker.getMissingCases(expression, bindingContext)
reportMissingCases(missingCases, expression)
}
super.visitWhenExpression(expression)
}

private fun reportMissingCases(
Expand Down

0 comments on commit 81d5dde

Please sign in to comment.