Skip to content

Commit

Permalink
Fix false positive when primary constructor has no arguments and a se…
Browse files Browse the repository at this point in the history
…condary constructor exists (#2717)

Closes #2690
  • Loading branch information
paul-dingemans authored Jun 26, 2024
1 parent 5fb8373 commit 6f0a313
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.pinterest.ktlint.ruleset.standard.rules

import com.pinterest.ktlint.rule.engine.core.api.AutocorrectDecision
import com.pinterest.ktlint.rule.engine.core.api.ElementType
import com.pinterest.ktlint.rule.engine.core.api.ElementType.ANNOTATION
import com.pinterest.ktlint.rule.engine.core.api.ElementType.ANNOTATION_ENTRY
import com.pinterest.ktlint.rule.engine.core.api.ElementType.CLASS
Expand Down Expand Up @@ -273,6 +274,19 @@ public class ClassSignatureRule :
// Allow:
// class Foo constructor() { ... }
it.prevCodeSibling()?.elementType == CONSTRUCTOR_KEYWORD
}?.takeUnless {
// Allow
// class Foo() {
// constructor(foo: String): this() {
// println(foo)
// }
// }
node
.findChildByType(CLASS_BODY)
?.findChildByType(ElementType.SECONDARY_CONSTRUCTOR)
?.findChildByType(ElementType.CONSTRUCTOR_DELEGATION_CALL)
?.firstChildNode
?.elementType == ElementType.CONSTRUCTOR_DELEGATION_REFERENCE
}?.let { parameterList ->
if (!dryRun) {
emit(parameterList.startOffset, "No parenthesis expected", true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1835,6 +1835,19 @@ class ClassSignatureRuleTest {
}
}

@Test
fun `Issue 2690 - Given an empty primary constructor and secondary constructor with delegation reference`() {
val code =
"""
class Foo() {
constructor(foo: String) : this() {
// N/A
}
}
""".trimIndent()
classSignatureWrappingRuleAssertThat(code).hasNoLintViolations()
}

private companion object {
const val UNEXPECTED_SPACES = " "
const val NO_SPACE = ""
Expand Down

0 comments on commit 6f0a313

Please # to comment.