Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

use primary constructor to fill non-primitive value #113

Merged
merged 1 commit into from
Jul 19, 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 @@ -298,8 +298,10 @@ open class FillClassFix(
}

val fqName = descriptor?.importableFqName?.asString()
val valueParameters =
descriptor?.constructors?.firstOrNull { it is ClassConstructorDescriptor }?.valueParameters
val valueParameters = descriptor?.constructors
?.sortedByDescending { it.isPrimary } // primary constructor first
?.firstOrNull { it is ClassConstructorDescriptor }
?.valueParameters
val argumentExpression = if (fqName != null && valueParameters != null) {
(factory.createExpression("$fqName()")).also {
val callExpression = it as? KtCallExpression ?: (it as? KtQualifiedExpression)?.callExpression
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,29 @@ class FillEmptyValueInspectionTest : BasePlatformTestCase() {
)
}

fun `test fill for non primitive types that has secondary constructor`() {
doAvailableTest(
"""
class A(a1: String, a2: Int) {
constructor(a1: String, a2: Int, a3: Boolean) : this(a1, a2)
}
class B(a: A)
fun test() {
B(<caret>)
}
""",
"""
class A(a1: String, a2: Int) {
constructor(a1: String, a2: Int, a3: Boolean) : this(a1, a2)
}
class B(a: A)
fun test() {
B(a = A(a1 = "", a2 = 0))
}
""",
)
}

fun `test fill for non primitive types and put argument on separate lines enabled`() {
doAvailableTest(
before = """
Expand Down