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

Don't report FunctionNaming when the function's name equals to the return type's name with type arguments #6605

Merged
merged 1 commit into from
Nov 4, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import io.gitlab.arturbosch.detekt.api.internal.Configuration
import io.gitlab.arturbosch.detekt.rules.isOverride
import io.gitlab.arturbosch.detekt.rules.naming.util.isContainingExcludedClassOrObject
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.KtUserType

/**
* Reports function names that do not follow the specified naming convention.
Expand Down Expand Up @@ -50,7 +51,7 @@ class FunctionNaming(config: Config = Config.empty) : Rule(config) {
val functionName = function.nameIdentifier?.text ?: return
if (!function.isContainingExcludedClassOrObject(excludeClassPattern) &&
!functionName.matches(functionPattern) &&
functionName != function.typeReference?.text
functionName != function.returnTypeName()
) {
report(
CodeSmell(
Expand All @@ -62,6 +63,8 @@ class FunctionNaming(config: Config = Config.empty) : Rule(config) {
}
}

private fun KtNamedFunction.returnTypeName() = (typeReference?.typeElement as? KtUserType)?.referencedName

companion object {
const val FUNCTION_PATTERN = "functionPattern"
const val EXCLUDE_CLASS_PATTERN = "excludeClassPattern"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ class FunctionNamingSpec {
assertThat(FunctionNaming().compileAndLint(code)).isEmpty()
}

@Test
fun `does not report when the function's name equals to the return type's name with type arguments`() {
val code = """
interface Foo<T>
fun <T> Foo(): Foo<T> = object : Foo<T> {}
""".trimIndent()
assertThat(FunctionNaming().compileAndLint(code)).isEmpty()
}

@Test
fun `flags functions with bad names inside overridden functions by default`() {
val code = """
Expand Down
Loading