Skip to content

Commit

Permalink
Improve the messaging for "DataClassDefaultValues"
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed Jan 30, 2024
1 parent 2fadfbb commit 07f2572
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class DataClassDefaultValuesRule(config: Config) : Rule(config) {
id = "DataClassDefaultValues",
severity = Severity.Maintainability,
description = "Data class properties should not have default values. " +
"Default values lead to implicit instance constructions and problems.",
"Default values lead to implicit creation of instances which leads to problems.",
debt = Debt.TWENTY_MINS,
)

Expand All @@ -33,8 +33,10 @@ class DataClassDefaultValuesRule(config: Config) : Rule(config) {
}

private fun failureMessage(klass: KtClass, parameter: KtParameter): String = buildString {
append("Data class '${klass.name}' should not have default values for properties. ")
val className = klass.name
append("Data class '$className' should not have default values. ")
append("Found default value for property '${Message.parameter(parameter)}'. ")
append("This can lead to implicit instance constructions and problems.")
append("This allows for instances of '$className' to be created without explicitly specifying all properties, ")
append("potentially leading to unintended or inconsistent states.")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal class DataClassDefaultValuesRuleTest(private val env: KotlinCoreEnviron
findings shouldHaveSize 1
val message = findings.first().message
message shouldBe """
Data class 'A' should not have default values for properties. Found default value for property 'x: Int = 42'. This can lead to implicit instance constructions and problems.
Data class 'A' should not have default values. Found default value for property 'x: Int = 42'. This allows for instances of 'A' to be created without explicitly specifying all properties, potentially leading to unintended or inconsistent states.
""".trimIndent()
}

Expand Down

0 comments on commit 07f2572

Please sign in to comment.