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

Fix false positive in no-semi rule for enum class without enum entries #2774

Merged
merged 1 commit into from
Aug 20, 2024
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
@@ -1,17 +1,21 @@
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.CLASS
import com.pinterest.ktlint.rule.engine.core.api.ElementType.CLASS_BODY
import com.pinterest.ktlint.rule.engine.core.api.ElementType.ENUM_ENTRY
import com.pinterest.ktlint.rule.engine.core.api.ElementType.ENUM_KEYWORD
import com.pinterest.ktlint.rule.engine.core.api.ElementType.OBJECT_KEYWORD
import com.pinterest.ktlint.rule.engine.core.api.ElementType.SEMICOLON
import com.pinterest.ktlint.rule.engine.core.api.Rule.VisitorModifier.RunAfterRule
import com.pinterest.ktlint.rule.engine.core.api.RuleId
import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint
import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint.Status.STABLE
import com.pinterest.ktlint.rule.engine.core.api.hasModifier
import com.pinterest.ktlint.rule.engine.core.api.ifAutocorrectAllowed
import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpace
import com.pinterest.ktlint.rule.engine.core.api.lastChildLeafOrSelf
import com.pinterest.ktlint.rule.engine.core.api.nextCodeSibling
import com.pinterest.ktlint.rule.engine.core.api.nextLeaf
import com.pinterest.ktlint.rule.engine.core.api.parent
import com.pinterest.ktlint.rule.engine.core.api.prevCodeLeaf
Expand Down Expand Up @@ -114,6 +118,9 @@ public class NoSemicolonsRule :
if (node.treeParent.elementType == ENUM_ENTRY) {
return node.isLastCodeLeafBeforeClosingOfClassBody()
}
if (node.isEnumClassWithoutValues()) {
return false
}

return true
}
Expand All @@ -125,6 +132,15 @@ public class NoSemicolonsRule :
?.parent(CLASS_BODY)
?.lastChildLeafOrSelf()
?.prevCodeLeaf()

private fun ASTNode?.isEnumClassWithoutValues() =
this
?.takeIf { !it.isLastCodeLeafBeforeClosingOfClassBody() }
?.parent(CLASS_BODY)
?.takeIf { this == it.firstChildNode.nextCodeSibling() }
?.parent(CLASS)
?.hasModifier(ENUM_KEYWORD)
?: false
}

public val NO_SEMICOLONS_RULE_ID: RuleId = NoSemicolonsRule().ruleId
Original file line number Diff line number Diff line change
Expand Up @@ -473,4 +473,35 @@ class NoSemicolonsRuleTest {
""".trimIndent()
noSemicolonsRuleAssertThat(code).hasNoLintViolations()
}

@Test
fun `Issue 2771 - Given an enum class without enum entry, and containing some code after the semi then do not report a lint error`() {
val code =
"""
enum class Foo {
;

fun foo() {
// do something
}
}
""".trimIndent()
noSemicolonsRuleAssertThat(code).hasNoLintViolations()
}

@Test
fun `Issue 2771 - Given an enum class without enum entry, and some comment before and some code after the semi then do not report a lint error`() {
val code =
"""
enum class Foo {
// NO-OP
;

fun foo() {
// do something
}
}
""".trimIndent()
noSemicolonsRuleAssertThat(code).hasNoLintViolations()
}
}