Skip to content

Commit

Permalink
CUSTOM_GETTERS_SETTERS false positive on the override of fields (#1606)
Browse files Browse the repository at this point in the history
### What's done:
* added check for override getter

Closes #1504
  • Loading branch information
Cheshiriks authored Feb 1, 2023
1 parent 98cdfaa commit 7954882
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
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

0 comments on commit 7954882

Please sign in to comment.