Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NonBooleanPropertyPrefixedWithIs: Allow boolean functions #5285

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
}
}
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