mirror of
https://github.com/jlengrand/detekt.git
synced 2026-03-10 08:11:23 +00:00
Allow constants to be skipped for BooleanPropertyNaming rule (#5006)
Co-authored-by: Amit Dash <amitdash@Amit-Dash.local>
This commit is contained in:
@@ -12,6 +12,7 @@ import io.gitlab.arturbosch.detekt.api.internal.Configuration
|
|||||||
import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution
|
import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution
|
||||||
import io.gitlab.arturbosch.detekt.rules.fqNameOrNull
|
import io.gitlab.arturbosch.detekt.rules.fqNameOrNull
|
||||||
import io.gitlab.arturbosch.detekt.rules.identifierName
|
import io.gitlab.arturbosch.detekt.rules.identifierName
|
||||||
|
import io.gitlab.arturbosch.detekt.rules.isConstant
|
||||||
import io.gitlab.arturbosch.detekt.rules.isOverride
|
import io.gitlab.arturbosch.detekt.rules.isOverride
|
||||||
import org.jetbrains.kotlin.psi.KtCallableDeclaration
|
import org.jetbrains.kotlin.psi.KtCallableDeclaration
|
||||||
import org.jetbrains.kotlin.psi.KtParameter
|
import org.jetbrains.kotlin.psi.KtParameter
|
||||||
@@ -68,8 +69,9 @@ class BooleanPropertyNaming(config: Config = Config.empty) : Rule(config) {
|
|||||||
val typeName = getTypeName(declaration)
|
val typeName = getTypeName(declaration)
|
||||||
val isBooleanType =
|
val isBooleanType =
|
||||||
typeName == KOTLIN_BOOLEAN_TYPE_NAME || typeName == JAVA_BOOLEAN_TYPE_NAME
|
typeName == KOTLIN_BOOLEAN_TYPE_NAME || typeName == JAVA_BOOLEAN_TYPE_NAME
|
||||||
|
val isNonConstantBooleanType = isBooleanType && !declaration.isConstant()
|
||||||
|
|
||||||
if (isBooleanType && !name.contains(allowedPattern) && !isIgnoreOverridden(declaration)) {
|
if (isNonConstantBooleanType && !name.contains(allowedPattern) && !isIgnoreOverridden(declaration)) {
|
||||||
report(reportCodeSmell(declaration, name))
|
report(reportCodeSmell(declaration, name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -270,6 +270,18 @@ class BooleanPropertyNamingSpec(val env: KotlinCoreEnvironment) {
|
|||||||
assertThat(findings).hasSize(1)
|
assertThat(findings).hasSize(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `should not warn about Kotlin Boolean if it is a constant val`() {
|
||||||
|
val code = """
|
||||||
|
object Test {
|
||||||
|
const val CONSTANT_VAL_BOOLEAN = true
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
val findings = subject.compileAndLintWithContext(env, code)
|
||||||
|
|
||||||
|
assertThat(findings).hasSize(0)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `should warn about Kotlin Boolean override if isIgnoreOverridden is false`() {
|
fun `should warn about Kotlin Boolean override if isIgnoreOverridden is false`() {
|
||||||
val code = """
|
val code = """
|
||||||
|
|||||||
Reference in New Issue
Block a user