From ee880c83c43b5b23c94f38661046601a67cc29a3 Mon Sep 17 00:00:00 2001 From: Vfrolov Date: Wed, 1 Feb 2023 12:06:12 +0300 Subject: [PATCH] CUSTOM_GETTERS_SETTERS false positive on the override of fields ### What's done: * added check for override getter Closes #1504 --- .../rules/chapter6/CustomGetterSetterRule.kt | 7 ++++++- .../chapter6/CustomGetterSetterWarnTest.kt | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/CustomGetterSetterRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/CustomGetterSetterRule.kt index 496fd51a9f..2602bcf3a5 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/CustomGetterSetterRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/CustomGetterSetterRule.kt @@ -7,6 +7,7 @@ import org.cqfn.diktat.ruleset.utils.* import com.pinterest.ktlint.core.ast.ElementType.GET_KEYWORD import com.pinterest.ktlint.core.ast.ElementType.MODIFIER_LIST +import com.pinterest.ktlint.core.ast.ElementType.OVERRIDE_KEYWORD import com.pinterest.ktlint.core.ast.ElementType.PRIVATE_KEYWORD import com.pinterest.ktlint.core.ast.ElementType.PROPERTY_ACCESSOR import com.pinterest.ktlint.core.ast.ElementType.SET_KEYWORD @@ -30,6 +31,7 @@ class CustomGetterSetterRule(configRules: List) : DiktatRule( val getter = node.getFirstChildWithType(GET_KEYWORD) val setter = node.getFirstChildWithType(SET_KEYWORD) val isPrivateSetter = node.getFirstChildWithType(MODIFIER_LIST)?.hasAnyChildOfTypes(PRIVATE_KEYWORD) ?: false + val isOverrideGetter = node.treeParent.getFirstChildWithType(MODIFIER_LIST)?.hasAnyChildOfTypes(OVERRIDE_KEYWORD) ?: false setter?.let { // only private custom setters are allowed @@ -39,7 +41,10 @@ class CustomGetterSetterRule(configRules: List) : DiktatRule( } getter?.let { - CUSTOM_GETTERS_SETTERS.warn(configRules, emitWarn, isFixMode, getter.text, getter.startOffset, node) + // only override getter are allowed + if (!isOverrideGetter) { + CUSTOM_GETTERS_SETTERS.warn(configRules, emitWarn, isFixMode, getter.text, getter.startOffset, node) + } } } diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter6/CustomGetterSetterWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter6/CustomGetterSetterWarnTest.kt index ef8b1db3fd..7e76dd583f 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter6/CustomGetterSetterWarnTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter6/CustomGetterSetterWarnTest.kt @@ -49,6 +49,23 @@ class CustomGetterSetterWarnTest : LintTestBase(::CustomGetterSetterRule) { ) } + @Test + @Tag(CUSTOM_GETTERS_SETTERS) + fun `override getter`() { + lintMethod( + """ + |interface A { + | val a: Int + |} + | + |object B: A { + | override val a: int + | get() = 0 + |} + """.trimMargin(), + ) + } + @Test @Tag(CUSTOM_GETTERS_SETTERS) fun `exception case with private setter`() {