Skip to content

Commit

Permalink
Implement "NoImplicitFunctionReturnType"
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed Jan 30, 2024
1 parent 8e66c3e commit 3dbb8db
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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

Expand All @@ -8,7 +9,8 @@ class NoImplicitFunctionReturnTypeRule(config: Config) : Rule(config) {
override val issue = Issue(
"NoImplicitFunctionReturnType",
Severity.Warning,
"Functions and class methods should declare their return types explicitly to improve code readability and maintainability.",
"Functions and class methods should declare their return types explicitly " +
"to improve code readability and maintainability.",
Debt.FIVE_MINS
)

Expand All @@ -20,10 +22,16 @@ class NoImplicitFunctionReturnTypeRule(config: Config) : Rule(config) {
CodeSmell(
issue,
Entity.from(function),
"The function '${function.name}' should declare an explicit return type."
failureMessage(function)
)
)
}
}

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. ")
append("Changing the implementation of such function is error-prone and can lead to regressions.")
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.github.ivy.explicit.rule

import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext
import io.kotest.matchers.collections.shouldHaveSize
import io.kotest.matchers.shouldBe
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.junit.jupiter.api.Test

@KotlinCoreEnvironmentTest
internal class NoImplicitFunctionReturnTypeRuleTest(private val env: KotlinCoreEnvironment) {

@Test
fun `reports function with implicit return type`() {
val code = """
fun magicNumber() = 42
"""
val findings = NoImplicitFunctionReturnTypeRule(Config.empty).compileAndLintWithContext(env, code)
findings shouldHaveSize 1
val message = findings.first().message
message shouldBe """
The function 'magicNumber()' should declare an explicit return type. Implicit (missing) return types make the code harder to read and reason about. Changing the implementation of such function is error-prone and can lead to regressions.
""".trimIndent()
}

@Test
fun `reports class method with implicit return type`() {
val code = """
class A {
fun a() = "Hello, world!"
}
"""
val findings = NoImplicitFunctionReturnTypeRule(Config.empty).compileAndLintWithContext(env, code)
findings shouldHaveSize 1
}

@Test
fun `doesn't report class method with explicit return type`() {
val code = """
class A {
fun a(): String = "Hello, world!"
}
"""
val findings = NoImplicitFunctionReturnTypeRule(Config.empty).compileAndLintWithContext(env, code)
findings shouldHaveSize 0
}

@Test
fun `doesn't report function with explicit return type`() {
val code = """
fun a(): String = "Hello, world!"
"""
val findings = NoImplicitFunctionReturnTypeRule(Config.empty).compileAndLintWithContext(env, code)
findings shouldHaveSize 0
}
}

0 comments on commit 3dbb8db

Please sign in to comment.