Repository: detekt/detekt (kotlin)

Issue:
Potential false positives on BooleanPropertyNaming with constants
## Context
BooleanPropertyNaming considers naming of properties, while technically these `const val`s are properties, they also have a different naming convention and I have a feeling they rarely start with `IS_` prefix.

## Expected Behavior of the rule
I propose to exclude `const` from this check fully.

```kotlin
object WidgetPreferences { // Detekt: or a companion object

	/**
	 * User selection of whether to show the part of day or not.
	 *
	 * *type*: [Boolean]
	 * *description*: true=show
	 * *default*: [DEFAULT_SHOW_PART_OF_DAY]
	 */
	const val PREF_SHOW_PART_OF_DAY = "showPartOfDay"
	const val DEFAULT_SHOW_PART_OF_DAY = true // Detekt: BooleanPropertyNaming shouldn't be reported
}
```

Pull request description:
Fixes #4920

Patch:
diff --git a/detekt-rules-naming/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/naming/BooleanPropertyNaming.kt b/detekt-rules-naming/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/naming/BooleanPropertyNaming.kt
index e28cea260aaf..29ae0bbb0a03 100644
--- a/detekt-rules-naming/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/naming/BooleanPropertyNaming.kt
+++ b/detekt-rules-naming/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/naming/BooleanPropertyNaming.kt
@@ -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))
         }
     }


Test patch:
diff --git a/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/BooleanPropertyNamingSpec.kt b/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/BooleanPropertyNamingSpec.kt
index a6fa028720e7..0b112ef86c23 100644
--- a/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/BooleanPropertyNamingSpec.kt
+++ b/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/BooleanPropertyNamingSpec.kt
@@ -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 = """