Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data class rule: removing several cases from the scope of the inspection #1471

Merged
merged 8 commits into from
Jul 25, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ class DataClassesRule(configRules: List<RulesConfig>) : DiktatRule(
}

private fun handleClass(node: ASTNode) {
with((node.psi as KtClass)) {
if (isData() || isInterface()) {
return
}
if ((node.psi as KtClass).isDefinitelyNotDataClass()) {
return
}

if (node.canBeDataClass()) {
raiseWarn(node)
}
Expand Down Expand Up @@ -128,6 +127,12 @@ class DataClassesRule(configRules: List<RulesConfig>) : DiktatRule(
return true
}

/** we do not exclude inner classes and enums here as if they have no
Copy link
Member

@petertrr petertrr Jul 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we filter out inner classes and enums elsewhere:

return list.getChildren(null)
.none { it.elementType in badModifiers } &&

private val badModifiers = listOf(OPEN_KEYWORD, ABSTRACT_KEYWORD, INNER_KEYWORD, SEALED_KEYWORD, ENUM_KEYWORD)

Copy link
Member Author

@orchestr7 orchestr7 Jul 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kek

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also I have no idea, why "open" is prohibited

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Copy link
Member Author

@orchestr7 orchestr7 Jul 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but what is the idea? You have open class only with data and properties?
Why not abstract class in this case?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are no abstract fields, than diktat will suggest to change abstract to open ¯\(ツ)

* 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<ASTNode>): Boolean {
accessors.forEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`() {
Expand Down