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

CUSTOM_GETTERS_SETTERS false positive on the override of fields #1606

Merged
merged 1 commit into from
Feb 1, 2023
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
Expand Up @@ -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
Expand All @@ -30,6 +31,7 @@ class CustomGetterSetterRule(configRules: List<RulesConfig>) : 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
Expand All @@ -39,7 +41,10 @@ class CustomGetterSetterRule(configRules: List<RulesConfig>) : 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)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`() {
Expand Down