Skip to content

Commit

Permalink
Allow constants to be skipped for BooleanPropertyNaming rule (#5006)
Browse files Browse the repository at this point in the history
Co-authored-by: Amit Dash <amitdash@Amit-Dash.local>
  • Loading branch information
amitdash291 and Amit Dash committed Jun 29, 2022
1 parent bea15be commit 49c6d88
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
}
Expand Down
Expand Up @@ -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 = """
Expand Down

0 comments on commit 49c6d88

Please sign in to comment.