Skip to content

Commit

Permalink
NonBooleanPropertyPrefixedWithIs: Allow boolean functions (#5285)
Browse files Browse the repository at this point in the history
  • Loading branch information
brittanyberlanga committed Sep 8, 2022
1 parent 5e46099 commit f104e99
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Expand Up @@ -67,7 +67,7 @@ class NonBooleanPropertyPrefixedWithIs(config: Config = Config.empty) : Rule(con

if (!typeName.isNullOrEmpty() &&
isNotBooleanType &&
!type.isBooleanFunctionReference()
!type.isBooleanFunction()
) {
report(
reportCodeSmell(declaration, name, typeName)
Expand Down Expand Up @@ -96,9 +96,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()
}
}
Expand Up @@ -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
Expand Down

0 comments on commit f104e99

Please sign in to comment.