Allow constants to be skipped for BooleanPropertyNaming rule (#5006)

Co-authored-by: Amit Dash <amitdash@Amit-Dash.local>
This commit is contained in:
Amit Dash
2022-06-30 00:32:56 +05:30
committed by GitHub
parent bea15be120
commit 49c6d88d08
2 changed files with 15 additions and 1 deletions

View File

@@ -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.rules.fqNameOrNull
import io.gitlab.arturbosch.detekt.rules.identifierName
import io.gitlab.arturbosch.detekt.rules.isConstant
import io.gitlab.arturbosch.detekt.rules.isOverride
import org.jetbrains.kotlin.psi.KtCallableDeclaration
import org.jetbrains.kotlin.psi.KtParameter
@@ -68,8 +69,9 @@ class BooleanPropertyNaming(config: Config = Config.empty) : Rule(config) {
val typeName = getTypeName(declaration)
val isBooleanType =
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))
}
}

View File

@@ -270,6 +270,18 @@ class BooleanPropertyNamingSpec(val env: KotlinCoreEnvironment) {
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
fun `should warn about Kotlin Boolean override if isIgnoreOverridden is false`() {
val code = """