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 b690da49e8..af85ad03e8 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 @@ -14,6 +14,7 @@ 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.ElementType.TYPE_REFERENCE import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.tree.TokenSet import org.jetbrains.kotlin.psi.KtProperty @@ -34,6 +35,7 @@ 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 isNotExtensionProperty = leftValue.treePrev?.treePrev?.elementType != TYPE_REFERENCE val firstReferenceWithSameName = node .findAllDescendantsWithSpecificType(REFERENCE_EXPRESSION) .mapNotNull { it.findChildByType(IDENTIFIER) } @@ -47,7 +49,8 @@ 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 && firstReferenceWithSameName.treeParent.treeParent.elementType != CALL_EXPRESSION) { + val isNotCallExpression = firstReferenceWithSameName?.treeParent?.treeParent?.elementType != CALL_EXPRESSION + if (firstReferenceWithSameName != null && isContainLocalVarSameName && isNotCallExpression && isNotExtensionProperty) { 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 7b23f53d5c..3baf4044ea 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 @@ -125,11 +125,27 @@ class PropertyAccessorFieldsWarnTest : LintTestBase(::PropertyAccessorFields) { """ |class A { | - | val String.blaBla: String + | val blaBla: String | get() = "bla".blaBla("bla") | - | fun String.blaBla(string: String): String = this + string - | fun boo(string: String): String = string + string + | fun blaBla(string: String): String = this + string + | + |} + """.trimMargin() + ) + } + + @Test + @Tag(WarningNames.WRONG_NAME_OF_VARIABLE_INSIDE_ACCESSOR) + fun `shouldn't be triggered when the property is an extension property`() { + lintMethod( + """ + |class A { + | + | fun String.foo() = 42 + | val String.foo: Int + | get() = foo + | |} """.trimMargin() )