From 28788f6380b3935446f28e5245a576a2d009d649 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Fri, 30 Jul 2021 15:09:01 +0300 Subject: [PATCH 1/2] Interface vs Data class bug ### What's done: Fixed bug Added test --- .../rules/chapter6/classes/DataClassesRule.kt | 7 ++++--- .../ruleset/chapter6/DataClassesRuleWarnTest.kt | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/classes/DataClassesRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/classes/DataClassesRule.kt index 2f03d76afa..9c7bb746ba 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/classes/DataClassesRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/classes/DataClassesRule.kt @@ -14,6 +14,7 @@ import com.pinterest.ktlint.core.ast.ElementType.DATA_KEYWORD import com.pinterest.ktlint.core.ast.ElementType.ENUM_KEYWORD import com.pinterest.ktlint.core.ast.ElementType.FUN import com.pinterest.ktlint.core.ast.ElementType.INNER_KEYWORD +import com.pinterest.ktlint.core.ast.ElementType.INTERFACE_KEYWORD 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.PRIMARY_CONSTRUCTOR @@ -42,7 +43,7 @@ class DataClassesRule(configRules: List) : DiktatRule( } private fun handleClass(node: ASTNode) { - if (node.isDataClass()) { + if (node.isDataClassOrInterface()) { return } @@ -144,12 +145,12 @@ class DataClassesRule(configRules: List) : DiktatRule( } @Suppress("UnsafeCallOnNullableType") - private fun ASTNode.isDataClass(): Boolean { + private fun ASTNode.isDataClassOrInterface(): Boolean { if (hasChildOfType(MODIFIER_LIST)) { val list = getFirstChildWithType(MODIFIER_LIST)!! return list.getChildren(null).any { it.elementType == DATA_KEYWORD } } - return false + return hasChildOfType(INTERFACE_KEYWORD) } companion object { diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter6/DataClassesRuleWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter6/DataClassesRuleWarnTest.kt index e9638b1656..8bac0d9747 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter6/DataClassesRuleWarnTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter6/DataClassesRuleWarnTest.kt @@ -275,4 +275,18 @@ class DataClassesRuleWarnTest : LintTestBase(::DataClassesRule) { """.trimMargin() ) } + + @Test + @Tag(USE_DATA_CLASS) + fun `shouldn't trigger on interface`() { + lintMethod( + """ + |interface Credentials { + | val code: String + | val success: Boolean + | val message: String + |} + """.trimMargin() + ) + } } From 690fdfd06815c8cfe97ac9483b508e45c94fa6b1 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Fri, 30 Jul 2021 17:22:30 +0300 Subject: [PATCH 2/2] Interface vs Data class bug ### What's done: Fixed after review --- .../rules/chapter6/classes/DataClassesRule.kt | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/classes/DataClassesRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/classes/DataClassesRule.kt index 9c7bb746ba..6bc07f5f01 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/classes/DataClassesRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/classes/DataClassesRule.kt @@ -43,10 +43,11 @@ class DataClassesRule(configRules: List) : DiktatRule( } private fun handleClass(node: ASTNode) { - if (node.isDataClassOrInterface()) { - return + with((node.psi as KtClass)) { + if (isData() || isInterface()) { + return + } } - if (node.canBeDataClass()) { raiseWarn(node) } @@ -144,15 +145,6 @@ class DataClassesRule(configRules: List) : DiktatRule( return true } - @Suppress("UnsafeCallOnNullableType") - private fun ASTNode.isDataClassOrInterface(): Boolean { - if (hasChildOfType(MODIFIER_LIST)) { - val list = getFirstChildWithType(MODIFIER_LIST)!! - return list.getChildren(null).any { it.elementType == DATA_KEYWORD } - } - return hasChildOfType(INTERFACE_KEYWORD) - } - companion object { private val badModifiers = listOf(OPEN_KEYWORD, ABSTRACT_KEYWORD, INNER_KEYWORD, SEALED_KEYWORD, ENUM_KEYWORD) }