Skip to content

Commit

Permalink
Fix NoImplicitFunctionReturnType for unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed Mar 27, 2024
1 parent dbff4a9 commit 73ed95e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.github.ivy.explicit.rule
import com.github.ivy.explicit.util.Message
import io.gitlab.arturbosch.detekt.api.*
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.psiUtil.containingClass

class NoImplicitFunctionReturnTypeRule(config: Config) : Rule(config) {

Expand All @@ -16,6 +17,12 @@ class NoImplicitFunctionReturnTypeRule(config: Config) : Rule(config) {

override fun visitNamedFunction(function: KtNamedFunction) {
super.visitNamedFunction(function)

if (function.isInsideTestClass()) {
// Do not report tests
return
}

// Check if the function has an explicit return type
if (function.typeReference == null && !function.hasBlockBody() && !function.isLocal) {
report(
Expand All @@ -28,6 +35,10 @@ class NoImplicitFunctionReturnTypeRule(config: Config) : Rule(config) {
}
}

private fun KtNamedFunction.isInsideTestClass(): Boolean {
return containingClass()?.name?.endsWith("Test") == true
}

private fun failureMessage(function: KtNamedFunction): String = buildString {
append("The function '${Message.functionSignature(function)}' should declare an explicit return type. ")
append("Implicit (missing) return types make the code harder to read and reason about. ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,23 @@ internal class NoImplicitFunctionReturnTypeRuleTest(private val env: KotlinCoreE
// then
findings shouldHaveSize 0
}

@Test
fun `doesn't report tests with implicit return type`() {
// given
val code = """
class SomeTest {
@Test
fun `my test`() = runTest {
// doesn't matter
}
}
"""

// when
val findings = rule.compileAndLintWithContext(env, code)

// then
findings shouldHaveSize 0
}
}

0 comments on commit 73ed95e

Please sign in to comment.