From b111e787e65efe1534bf1d6d3cc718c842a8f0a7 Mon Sep 17 00:00:00 2001 From: Vfrolov Date: Mon, 28 Jun 2021 17:50:03 +0300 Subject: [PATCH] WRONG_NAME_OF_VARIABLE_INSIDE_ACCESSOR false positive trigger ### What's done: * fixed false positive trigger Closes #940 --- .../rules/chapter6/PropertyAccessorFields.kt | 10 +++++++++- .../chapter6/PropertyAccessorFieldsWarnTest.kt | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/PropertyAccessorFields.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/PropertyAccessorFields.kt index 7ac7671968..237eb28e8f 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/PropertyAccessorFields.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/PropertyAccessorFields.kt @@ -8,11 +8,14 @@ import org.cqfn.diktat.ruleset.utils.isGoingAfter import com.pinterest.ktlint.core.ast.ElementType.BLOCK import com.pinterest.ktlint.core.ast.ElementType.DOT_QUALIFIED_EXPRESSION +import com.pinterest.ktlint.core.ast.ElementType.FILE +import com.pinterest.ktlint.core.ast.ElementType.FUN import com.pinterest.ktlint.core.ast.ElementType.IDENTIFIER import com.pinterest.ktlint.core.ast.ElementType.PROPERTY import com.pinterest.ktlint.core.ast.ElementType.PROPERTY_ACCESSOR import com.pinterest.ktlint.core.ast.ElementType.REFERENCE_EXPRESSION import com.pinterest.ktlint.core.ast.ElementType.THIS_EXPRESSION +import com.pinterest.ktlint.core.ast.parent import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.tree.TokenSet import org.jetbrains.kotlin.psi.KtProperty @@ -33,6 +36,11 @@ class PropertyAccessorFields(configRules: List) : DiktatRule( // fixme should use shadow-check when it will be done private fun checkPropertyAccessor(node: ASTNode) { val leftValue = node.treeParent.findChildByType(IDENTIFIER) ?: return + val funNames: List = node + .parent(FILE) + ?.findAllDescendantsWithSpecificType(FUN) + ?.mapNotNull { it.findChildByType(IDENTIFIER) } + ?.map { it.text } ?: emptyList() val firstReferenceWithSameName = node .findAllDescendantsWithSpecificType(REFERENCE_EXPRESSION) .mapNotNull { it.findChildByType(IDENTIFIER) } @@ -46,7 +54,7 @@ class PropertyAccessorFields(configRules: List) : DiktatRule( ?.getChildren(TokenSet.create(PROPERTY)) ?.filter { (it.psi as KtProperty).nameIdentifier?.text == leftValue.text } ?.none { firstReferenceWithSameName?.isGoingAfter(it) ?: false } ?: true - if (firstReferenceWithSameName != null && isContainLocalVarSameName) { + if (firstReferenceWithSameName != null && isContainLocalVarSameName && funNames.all { it != firstReferenceWithSameName.text }) { WRONG_NAME_OF_VARIABLE_INSIDE_ACCESSOR.warn(configRules, emitWarn, isFixMode, node.text, node.startOffset, node) } } diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter6/PropertyAccessorFieldsWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter6/PropertyAccessorFieldsWarnTest.kt index c9e61fac04..0bd8dedc43 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter6/PropertyAccessorFieldsWarnTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter6/PropertyAccessorFieldsWarnTest.kt @@ -117,4 +117,21 @@ class PropertyAccessorFieldsWarnTest : LintTestBase(::PropertyAccessorFields) { LintError(24, 4, ruleId, "${WRONG_NAME_OF_VARIABLE_INSIDE_ACCESSOR.warnText()} set(value) {...") ) } + + @Test + @Tag(WarningNames.WRONG_NAME_OF_VARIABLE_INSIDE_ACCESSOR) + fun `shouldn't be triggered when there's a method with the same name as the property`() { + lintMethod( + """ + |class A { + | + | val String.blaBla: String + | get() = "bla".blaBla("bla") + | + | fun String.blaBla(string: String): String = this + string + | fun boo(string: String): String = string + string + |} + """.trimMargin() + ) + } }