diff --git a/CHANGELOG.md b/CHANGELOG.md index b0e0cf7f53..2e13b00a42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](https://semver.org/). * Do not flag a (potential) mutable extension property in case the getter is annotated or prefixed with a modifier `property-naming` ([#2024](https://github.com/pinterest/ktlint/issues/2024)) * Do not merge an annotated expression body with the function signature even if it fits on a single line ([#2043](https://github.com/pinterest/ktlint/issues/2043)) +* Ignore property with name `serialVersionUID` in `property-naming` ([#2045](https://github.com/pinterest/ktlint/issues/2045)) ### Changed diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/PropertyNamingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/PropertyNamingRule.kt index d3d4a00131..79f1e477a7 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/PropertyNamingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/PropertyNamingRule.kt @@ -81,7 +81,13 @@ public class PropertyNamingRule : ) { identifier .text - .takeUnless { it.matches(SCREAMING_SNAKE_CASE_REGEXP) } + .takeUnless { + // Allow + // object Foo { + // private const val serialVersionUID: Long = 123 + // } + it == SERIAL_VERSION_UID_PROPERTY_NAME + }?.takeUnless { it.matches(SCREAMING_SNAKE_CASE_REGEXP) } ?.let { emit( identifier.startOffset, @@ -143,6 +149,7 @@ public class PropertyNamingRule : val LOWER_CAMEL_CASE_REGEXP = "[a-z][a-zA-Z0-9]*".regExIgnoringDiacriticsAndStrokesOnLetters() val SCREAMING_SNAKE_CASE_REGEXP = "[A-Z][_A-Z0-9]*".regExIgnoringDiacriticsAndStrokesOnLetters() val BACKING_PROPERTY_LOWER_CAMEL_CASE_REGEXP = "_[a-z][a-zA-Z0-9]*".regExIgnoringDiacriticsAndStrokesOnLetters() + const val SERIAL_VERSION_UID_PROPERTY_NAME = "serialVersionUID" } } diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/PropertyNamingRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/PropertyNamingRuleTest.kt index 99768a1aab..bc0271b805 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/PropertyNamingRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/PropertyNamingRuleTest.kt @@ -1,6 +1,7 @@ package com.pinterest.ktlint.ruleset.standard.rules import com.pinterest.ktlint.test.KtLintAssertThat +import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.ValueSource @@ -172,4 +173,39 @@ class PropertyNamingRuleTest { """.trimIndent() propertyNamingRuleAssertThat(code).hasNoLintViolations() } + + @Nested + inner class `Issue 2017 - Given property is serialVersionUID` { + @Test + fun `Given property is present in companion object`() { + val code = + """ + class Foo1 { + companion object { + private const val serialVersionUID: Long = 123 + } + } + class Foo2 { + companion object { + private const val serialVersionUID = 123L + } + } + """.trimIndent() + propertyNamingRuleAssertThat(code).hasNoLintViolations() + } + + @Test + fun `Given property defined object is private const`() { + val code = + """ + object Foo1 { + private const val serialVersionUID: Long = 123 + } + object Foo2 { + private const val serialVersionUID = 123L + } + """.trimIndent() + propertyNamingRuleAssertThat(code).hasNoLintViolations() + } + } }