From 2de5655c6cd03388ba5cee3c82bb8fa570b3f63e Mon Sep 17 00:00:00 2001 From: brittanyberlanga Date: Wed, 7 Sep 2022 23:16:47 -0700 Subject: [PATCH 1/2] Add NonBooleanPropertyPrefixedWithIs tests --- .../NonBooleanPropertyWithPrefixIsSpec.kt | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/NonBooleanPropertyWithPrefixIsSpec.kt b/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/NonBooleanPropertyWithPrefixIsSpec.kt index c8b619cd96b..03a7be248a5 100644 --- a/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/NonBooleanPropertyWithPrefixIsSpec.kt +++ b/detekt-rules-naming/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/naming/NonBooleanPropertyWithPrefixIsSpec.kt @@ -250,6 +250,30 @@ class NonBooleanPropertyWithPrefixIsSpec(val env: KotlinCoreEnvironment) { assertThat(findings).hasSize(1) } + + @Test + fun `should not detect boolean function`() { + val code = """ + class O { + val isEnabled: () -> Boolean = { true } + } + """.trimIndent() + val findings = subject.compileAndLintWithContext(env, code) + + assertThat(findings).isEmpty() + } + + @Test + fun `should not detect boolean function with parameter`() { + val code = """ + class O { + val isEnabled: (String) -> Boolean = { true } + } + """.trimIndent() + val findings = subject.compileAndLintWithContext(env, code) + + assertThat(findings).isEmpty() + } } @Nested From 28d466b8a7ee53a648d019602f3a65ddb3fc0d39 Mon Sep 17 00:00:00 2001 From: brittanyberlanga Date: Wed, 7 Sep 2022 23:17:21 -0700 Subject: [PATCH 2/2] Expand NonBooleanPropertyPrefixedWithIs to support boolean functions with parameters --- .../detekt/rules/naming/NonBooleanPropertyPrefixedWithIs.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/detekt-rules-naming/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/naming/NonBooleanPropertyPrefixedWithIs.kt b/detekt-rules-naming/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/naming/NonBooleanPropertyPrefixedWithIs.kt index e8c17808be6..3e59f9c438a 100644 --- a/detekt-rules-naming/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/naming/NonBooleanPropertyPrefixedWithIs.kt +++ b/detekt-rules-naming/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/naming/NonBooleanPropertyPrefixedWithIs.kt @@ -72,7 +72,7 @@ class NonBooleanPropertyPrefixedWithIs(config: Config = Config.empty) : Rule(con if (!typeName.isNullOrEmpty() && isNotBooleanType && - !type.isBooleanFunctionReference() + !type.isBooleanFunction() ) { report( reportCodeSmell(declaration, name, typeName) @@ -101,9 +101,9 @@ class NonBooleanPropertyPrefixedWithIs(config: Config = Config.empty) : Rule(con fqNameOrNull() ?.asString() - private fun KotlinType.isBooleanFunctionReference(): Boolean { + private fun KotlinType.isBooleanFunction(): Boolean { if (!isFunctionOrKFunctionTypeWithAnySuspendability) return false - return arguments.count() == 1 && arguments[0].type.isBoolean() + return arguments.isNotEmpty() && arguments.last().type.isBoolean() } }