Skip to content

Commit

Permalink
Fix: CLASS_SHOULD_NOT_BE_ABSTRACT breaks code when abstract class imp…
Browse files Browse the repository at this point in the history
…lements an interface (#1200)

### What's done:
* Class can have `abstract` modifier if it implements interface, don't trigger on such cases
  • Loading branch information
kgevorkyan authored Jan 31, 2022
1 parent 854cb18 commit 749587a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.pinterest.ktlint.core.ast.ElementType.MODIFIER_LIST
import com.pinterest.ktlint.core.ast.ElementType.OPEN_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.PROPERTY
import com.pinterest.ktlint.core.ast.ElementType.SUPER_TYPE_CALL_ENTRY
import com.pinterest.ktlint.core.ast.ElementType.SUPER_TYPE_ENTRY
import com.pinterest.ktlint.core.ast.ElementType.SUPER_TYPE_LIST
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
Expand All @@ -35,6 +36,7 @@ class AbstractClassesRule(configRules: List<RulesConfig>) : DiktatRule(

// If an abstract class extends another class, than that base class can be abstract too.
// Then this class must have `abstract` modifier even if it doesn't have any abstract members.
// Class also can have `abstract` modifier if it implements interface
if (node.hasAbstractModifier() && node.isNotSubclass()) {
handleAbstractClass(classBody, node)
}
Expand All @@ -45,7 +47,7 @@ class AbstractClassesRule(configRules: List<RulesConfig>) : DiktatRule(
getFirstChildWithType(MODIFIER_LIST)?.hasChildOfType(ABSTRACT_KEYWORD) ?: false

private fun ASTNode.isNotSubclass(): Boolean = findChildByType(SUPER_TYPE_LIST)?.children()?.filter {
it.elementType == SUPER_TYPE_CALL_ENTRY
it.elementType == SUPER_TYPE_CALL_ENTRY || it.elementType == SUPER_TYPE_ENTRY
}?.none() ?: true

@Suppress("UnsafeCallOnNullableType")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,13 @@ class AbstractClassesWarnTest : LintTestBase(::AbstractClassesRule) {

@Test
@Tag(CLASS_SHOULD_NOT_BE_ABSTRACT)
fun `should trigger on classes that implement interfaces`() {
fun `should not trigger on classes that implement interfaces`() {
lintMethod(
"""
|abstract class Example: Base {
| fun foo() {}
|}
""".trimMargin(),
LintError(1, 30, ruleId, "[CLASS_SHOULD_NOT_BE_ABSTRACT] class should not be abstract, because it has no abstract functions: Example", true)
)
}
}

0 comments on commit 749587a

Please sign in to comment.