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
Added factory function check to MemberNameEqualsClassName #653
Added factory function check to MemberNameEqualsClassName #653
Conversation
Factory functions that create an instance of the class can now have the same name as the class.
|
||
private fun isFactoryMethod(function: KtNamedFunction, klass: KtClass): Boolean { | ||
val typeReference = function.typeReference | ||
return typeReference == null && function.bodyExpression !is KtBlockExpression |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This ignores functions which return a single expression since detekt cannot infer the type (at the moment).
Consider the following code for example
open class A {
companion object {
// return type = A
fun a(condition: Boolean) = if (condition) B() else C()
}
}
class B: A()
class C: A()
} | ||
private fun getMisnamedCompanionObjectMembers(klass: KtClass): List<KtNamedDeclaration> { | ||
val list = mutableListOf<KtNamedDeclaration>() | ||
klass.companionObjects.forEach { list.addAll(getMisnamedMembers(it, klass.name)) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be just a flatMap
-Funktion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Implements the feature mentioned in #652