generated from detekt/detekt-custom-rule-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADd the "DataClassDefaultValues" rule
- Loading branch information
1 parent
c9bedd9
commit 6096461
Showing
6 changed files
with
123 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/kotlin/com/github/ivy/explicit/rule/DataClassDefaultValuesRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.github.ivy.explicit.rule | ||
|
||
import com.github.ivy.explicit.util.Message | ||
import io.gitlab.arturbosch.detekt.api.* | ||
import org.jetbrains.kotlin.psi.KtClass | ||
import org.jetbrains.kotlin.psi.KtParameter | ||
|
||
class DataClassDefaultValuesRule(config: Config) : Rule(config) { | ||
|
||
override val issue = Issue( | ||
id = "DataClassDefaultValues", | ||
severity = Severity.Maintainability, | ||
description = "Data class properties should not have default values. " + | ||
"Default values lead to implicit instance constructions and problems.", | ||
debt = Debt.TWENTY_MINS, | ||
) | ||
|
||
override fun visitClass(klass: KtClass) { | ||
super.visitClass(klass) | ||
if (klass.isData()) { | ||
klass.primaryConstructorParameters.filter { | ||
it.hasDefaultValue() | ||
}.forEach { parameter -> | ||
report( | ||
CodeSmell( | ||
issue = issue, | ||
entity = Entity.from(parameter), | ||
message = failureMessage(klass, parameter) | ||
) | ||
) | ||
} | ||
} | ||
} | ||
|
||
private fun failureMessage(klass: KtClass, parameter: KtParameter): String = buildString { | ||
append("Data class '${klass.name}' should not have default values for properties. ") | ||
append("Found default value for property '${Message.parameter(parameter)}'. ") | ||
append("This can lead to implicit instance constructions and problems.") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
IvyExplicit: | ||
DataClassFunctions: | ||
active: true | ||
DataClassDefaultValues: | ||
active: true |
64 changes: 64 additions & 0 deletions
64
src/test/kotlin/com/github/ivy/explicit/rule/DataClassDefaultValuesRuleTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.github.ivy.explicit.rule | ||
|
||
import io.gitlab.arturbosch.detekt.api.Config | ||
import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest | ||
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext | ||
import io.kotest.matchers.collections.shouldHaveSize | ||
import io.kotest.matchers.shouldBe | ||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment | ||
import org.junit.jupiter.api.Test | ||
|
||
@KotlinCoreEnvironmentTest | ||
internal class DataClassDefaultValuesRuleTest(private val env: KotlinCoreEnvironment) { | ||
|
||
@Test | ||
fun `reports data class with a default value`() { | ||
val code = """ | ||
data class A( | ||
val x: Int = 42 | ||
) | ||
""" | ||
val findings = DataClassDefaultValuesRule(Config.empty).compileAndLintWithContext(env, code) | ||
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. | ||
""".trimIndent() | ||
} | ||
|
||
@Test | ||
fun `reports data class with a override default value`() { | ||
val code = """ | ||
data class A( | ||
override val x: Int = 0, | ||
override val y: Int = 0, | ||
): Point | ||
""" | ||
val findings = DataClassDefaultValuesRule(Config.empty).compileAndLintWithContext(env, code) | ||
findings shouldHaveSize 2 | ||
} | ||
|
||
@Test | ||
fun `doesn't report class with a default value`() { | ||
val code = """ | ||
class A( | ||
val x: Int = 42 | ||
) | ||
""" | ||
val findings = DataClassDefaultValuesRule(Config.empty).compileAndLintWithContext(env, code) | ||
findings shouldHaveSize 0 | ||
} | ||
|
||
@Test | ||
fun `doesn't report data class without default values`() { | ||
val code = """ | ||
class Point( | ||
val x: Double, | ||
val y: Double, | ||
) | ||
""" | ||
val findings = DataClassDefaultValuesRule(Config.empty).compileAndLintWithContext(env, code) | ||
findings shouldHaveSize 0 | ||
} | ||
|
||
} |