Skip to content

Commit

Permalink
Hotfix for Data Classes rule (#1445)
Browse files Browse the repository at this point in the history
### What's done:
- covered cases when data class does not have a body: class (val a: Int)
Previously we handled only class (val a: Int) {} because of the invalid logic for `hasAppropriateClassBody`
  • Loading branch information
orchestr7 authored Jul 13, 2022
1 parent 98ea490 commit ad8718b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ class SmartCastRule(configRules: List<RulesConfig>) : DiktatRule(
* @property type a type to which the reference is being cast
* @property node a node that holds the entire expression
*/
class AsExpressions(
data class AsExpressions(
val identifier: String,
val type: String,
val node: ASTNode
Expand All @@ -262,7 +262,7 @@ class SmartCastRule(configRules: List<RulesConfig>) : DiktatRule(
* @property identifier a reference that is checked
* @property type a type with which the reference is being compared
*/
class IsExpressions(val identifier: String, val type: String)
data class IsExpressions(val identifier: String, val type: String)

companion object {
const val NAME_ID = "smart-cast-rule"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class DataClassesRule(configRules: List<RulesConfig>) : DiktatRule(
?: false &&
getFirstChildWithType(SUPER_TYPE_LIST) == null
}
return classBody?.getAllChildrenWithType(FUN)?.isEmpty() ?: false &&
return classBody?.getFirstChildWithType(FUN) == null &&
getFirstChildWithType(SUPER_TYPE_LIST) == null &&
// if there is any prop with logic in accessor then don't recommend to convert class to data class
classBody?.let(::areGoodProps)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ class DataClassesRuleWarnTest : LintTestBase(::DataClassesRule) {
)
}

@Test
@Tag(USE_DATA_CLASS)
fun `_regression_ trigger on default class without a body`() {
lintMethod(
"""
|class Some(val a: Int = 5)
|
""".trimMargin(),
LintError(1, 1, ruleId, "${Warnings.USE_DATA_CLASS.warnText()} Some")
)
}

@Test
@Tag(USE_DATA_CLASS)
fun `should trigger - dont forget to consider this class in fix`() {
Expand Down

0 comments on commit ad8718b

Please sign in to comment.