Skip to content

Commit

Permalink
Avoid false positives in MemberNameEqualsClassName (#4329)
Browse files Browse the repository at this point in the history
  • Loading branch information
BraisGabin committed Nov 25, 2021
1 parent a1150bc commit adcca36
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
Expand Up @@ -113,12 +113,13 @@ class MemberNameEqualsClassName(config: Config = Config.empty) : Rule(config) {
val refName = (typeReference.typeElement as? KtUserType)?.referencedName ?: typeReference.text
refName == klass.name
}
function.bodyExpression is KtBlockExpression -> false
function.bodyExpression !is KtBlockExpression && bindingContext != BindingContext.EMPTY -> {
val functionDescriptor = bindingContext[BindingContext.FUNCTION, function]
val classDescriptor = bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, klass]
functionDescriptor?.returnType?.constructor?.declarationDescriptor == classDescriptor
}
else -> false
else -> true // We don't know if it is or not a factory. We assume it is a factory to avoid false-positives
}
}
}
Expand Up @@ -255,7 +255,7 @@ class MemberNameEqualsClassNameSpec : Spek({
assertThat(MemberNameEqualsClassName().compileAndLintWithContext(env, code)).isEmpty()
}

it("doesn't report a body-less factory function") {
context("doesn't report a body-less factory function") {
val code = """
open class A {
companion object {
Expand All @@ -267,7 +267,14 @@ class MemberNameEqualsClassNameSpec : Spek({
class C: A()
"""
assertThat(MemberNameEqualsClassName().compileAndLintWithContext(env, code)).isEmpty()

it("with type solving") {
assertThat(MemberNameEqualsClassName().compileAndLintWithContext(env, code)).isEmpty()
}

it("without type solving") {
assertThat(MemberNameEqualsClassName().compileAndLint(code)).isEmpty()
}
}
}
}
Expand Down

0 comments on commit adcca36

Please sign in to comment.