Skip to content

Commit

Permalink
Adapt WHEN_WITHOUT_ELSE according to new features of Kotlin 1.6
Browse files Browse the repository at this point in the history
### What's done:
 * changed behavior with expressions that look like enum entries of when: now if all the entries look like enum entries, WHEN_WITHOUT_ELSE rule is ignored
 * added tests

(#1149)
  • Loading branch information
sanyavertolet committed Feb 3, 2022
1 parent 61db505 commit 7fc35bd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ package org.cqfn.diktat.ruleset.rules.chapter3
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.ruleset.constants.Warnings.WHEN_WITHOUT_ELSE
import org.cqfn.diktat.ruleset.rules.DiktatRule
import org.cqfn.diktat.ruleset.utils.appendNewlineMergingWhiteSpace
import org.cqfn.diktat.ruleset.utils.getAllChildrenWithType
import org.cqfn.diktat.ruleset.utils.getFirstChildWithType
import org.cqfn.diktat.ruleset.utils.hasChildOfType
import org.cqfn.diktat.ruleset.utils.hasParent
import org.cqfn.diktat.ruleset.utils.isBeginByNewline

import com.pinterest.ktlint.core.ast.ElementType
import com.pinterest.ktlint.core.ast.ElementType.ARROW
Expand All @@ -24,7 +30,6 @@ import com.pinterest.ktlint.core.ast.ElementType.WHEN_CONDITION_IS_PATTERN
import com.pinterest.ktlint.core.ast.ElementType.WHEN_CONDITION_WITH_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.WHEN_ENTRY
import com.pinterest.ktlint.core.ast.prevSibling
import org.cqfn.diktat.ruleset.utils.*
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.CompositeElement
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
Expand Down Expand Up @@ -82,7 +87,7 @@ class WhenMustHaveElseRule(configRules: List<RulesConfig>) : DiktatRule(
it.getAllChildrenWithType(WHEN_CONDITION_WITH_EXPRESSION)
}.flatten()

val areOnlyEnumEntriesWithExpressions = if(conditionsWithExpression.isNotEmpty()) {
val areOnlyEnumEntriesWithExpressions = if (conditionsWithExpression.isNotEmpty()) {
conditionsWithExpression.all {
it.hasChildOfType(DOT_QUALIFIED_EXPRESSION) || it.hasChildOfType(REFERENCE_EXPRESSION)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ internal fun createWithFile(runConfiguration: RunConfiguration,
private fun foo(node: ASTNode) {
when (node.elementType) {
CLASS, FUN, PRIMARY_CONSTRUCTOR, SECONDARY_CONSTRUCTOR -> checkAnnotation(node)
else -> {
// this is a generated else block
}
}
val qwe = a && b
val qwe = a &&
Expand Down

0 comments on commit 7fc35bd

Please sign in to comment.