diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BackingPropertyNamingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BackingPropertyNamingRule.kt index cc81eda298..ce6118bb44 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BackingPropertyNamingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BackingPropertyNamingRule.kt @@ -4,6 +4,7 @@ import com.pinterest.ktlint.rule.engine.core.api.AutocorrectDecision import com.pinterest.ktlint.rule.engine.core.api.ElementType.FUN import com.pinterest.ktlint.rule.engine.core.api.ElementType.IDENTIFIER import com.pinterest.ktlint.rule.engine.core.api.ElementType.INTERNAL_KEYWORD +import com.pinterest.ktlint.rule.engine.core.api.ElementType.OVERRIDE_KEYWORD import com.pinterest.ktlint.rule.engine.core.api.ElementType.PRIVATE_KEYWORD import com.pinterest.ktlint.rule.engine.core.api.ElementType.PROPERTY import com.pinterest.ktlint.rule.engine.core.api.ElementType.PROTECTED_KEYWORD @@ -53,7 +54,10 @@ public class BackingPropertyNamingRule : property .findChildByType(IDENTIFIER) ?.takeIf { it.text.startsWith("_") } - ?.let { identifier -> + ?.takeUnless { + // Do not report overridden properties as they can only be changed by changing the base property + it.treeParent.hasModifier(OVERRIDE_KEYWORD) + }?.let { identifier -> visitBackingProperty(identifier, emit) } } diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BackingPropertyNamingRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BackingPropertyNamingRuleTest.kt index 40cca61c9d..6d33a7cdcf 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BackingPropertyNamingRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BackingPropertyNamingRuleTest.kt @@ -345,4 +345,18 @@ class BackingPropertyNamingRuleTest { """.trimIndent() backingPropertyNamingRuleAssertThat(code).hasNoLintViolations() } + + @Test + fun `Issue 2748 - Given an override property with name starting with '_' then do not report a violation`() { + val code = + """ + // The property "__foo" in example below can be defined in an external dependency, which can not be changed. Even in case it is + // a (internal) dependency that can be changed, the violation should only be reported at the base property, but on the overrides + val fooBar = + object : FooBar { + override val __foo = "foo" + } + """.trimIndent() + backingPropertyNamingRuleAssertThat(code).hasNoLintViolations() + } }