From 20b3e2c5047c45e84430ad37786cbbfba88193b4 Mon Sep 17 00:00:00 2001 From: Andrey Kuleshov Date: Fri, 22 Jul 2022 14:43:06 +0300 Subject: [PATCH] Data class rule: removing several cases from the scope of the inspection ### What's done: - Removed annotation, value, sealed, inline, inner classes from the scope - Added tests --- .../rules/chapter6/classes/DataClassesRule.kt | 13 +++-- .../chapter6/DataClassesRuleWarnTest.kt | 52 +++++++++++++++++++ 2 files changed, 61 insertions(+), 4 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 eea79a7662..b5851ba92b 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 @@ -42,11 +42,10 @@ class DataClassesRule(configRules: List) : DiktatRule( } private fun handleClass(node: ASTNode) { - with((node.psi as KtClass)) { - if (isData() || isInterface()) { - return - } + if ((node.psi as KtClass).isDefinitelyNotADataClass()) { + return } + if (node.canBeDataClass()) { raiseWarn(node) } @@ -128,6 +127,12 @@ class DataClassesRule(configRules: List) : DiktatRule( return true } + /** we do not exclude inner classes and enums here as if they have no + * methods, then we definitely can refactor the code and make them data classes + **/ + private fun KtClass.isDefinitelyNotDataClass() = + isValue() || isAnnotation() || isInterface() || isData() || isSealed() || isInline() || isInner() + @Suppress("UnsafeCallOnNullableType") private fun areGoodAccessors(accessors: List): Boolean { accessors.forEach { 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 3b07445909..2f24bac51d 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 @@ -288,6 +288,58 @@ class DataClassesRuleWarnTest : LintTestBase(::DataClassesRule) { ) } + @Test + @Tag(USE_DATA_CLASS) + fun `annotation classes bug`() { + lintMethod( + """ + |@Retention(AnnotationRetention.SOURCE) + |@Target(AnnotationTarget.CLASS) + |annotation class NavGraphDestination( + | val name: String = Defaults.NULL, + | val routePrefix: String = Defaults.NULL, + | val deepLink: Boolean = false, + |) { + | object Defaults { + | const val NULL = "@null" + | } + |} + """.trimMargin() + ) + } + + @Test + @Tag(USE_DATA_CLASS) + fun `value or inline classes bug`() { + lintMethod( + """ + |@JvmInline + |value class Password(private val s: String) + |val securePassword = Password("Don't try this in production") + """.trimMargin() + ) + } + + @Test + @Tag(USE_DATA_CLASS) + fun `sealed classes bug`() { + lintMethod( + """ + |sealed class Password(private val s: String) + """.trimMargin() + ) + } + + @Test + @Tag(USE_DATA_CLASS) + fun `inner classes bug`() { + lintMethod( + """ + |inner class Password(private val s: String) + """.trimMargin() + ) + } + @Test @Tag(USE_DATA_CLASS) fun `shouldn't trigger on interface`() {