Repository: analysis-dev/diktat (kotlin)

Issue:
WRONG_NAME_OF_VARIABLE_INSIDE_ACCESSOR false positive trigger
## Describe the bug
See e.g. the following code:
```
val String.blaBla: String
    get() = "bla".blaBla("bla")

fun String.blaBla(string: String): String = this + string
```

WRONG_NAME_OF_VARIABLE_INSIDE_ACCESSOR is triggered, saying that `field` keyword should be used instead of property name inside property accessors. But it seems that it mistakes the `blaBla` method name for the property name because they are the same in this case.

## Expected behavior
WRONG_NAME_OF_VARIABLE_INSIDE_ACCESSOR shouldn't be triggered when there's a method with the same name as the property

## Observed behavior
WRONG_NAME_OF_VARIABLE_INSIDE_ACCESSOR is triggered when there's a method with the same name as the property

## Steps to Reproduce
1. Create an extension variable;
2. Call a method, the name of which is the same as the property itself, within the property getter;
3. Run `diktatCheck` to observe the supposed false positive trigger of WRONG_NAME_OF_VARIABLE_INSIDE_ACCESSOR.

## Environment information
* diktat version: 0.6.2
* build tool (maven/gradle): Gradle 6.7
* how is diktat run (CLI, plugin, etc.): gradle plugin
* kotlin version: 1.5.10
* operating system: Windows 10
* link to a project (if your project is public):

Pull request:
### What's done:
* fixed false positive trigger
Closes #940

Patch:
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..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
@@ -7,12 +7,14 @@ import org.cqfn.diktat.ruleset.utils.findAllDescendantsWithSpecificType
 import org.cqfn.diktat.ruleset.utils.isGoingAfter
 
 import com.pinterest.ktlint.core.ast.ElementType.BLOCK
+import com.pinterest.ktlint.core.ast.ElementType.CALL_EXPRESSION
 import com.pinterest.ktlint.core.ast.ElementType.DOT_QUALIFIED_EXPRESSION
 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.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
@@ -33,6 +35,7 @@ class PropertyAccessorFields(configRules: List<RulesConfig>) : 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) }
@@ -46,7 +49,8 @@ class PropertyAccessorFields(configRules: List<RulesConfig>) : DiktatRule(
             ?.getChildren(TokenSet.create(PROPERTY))
             ?.filter { (it.psi as KtProperty).nameIdentifier?.text == leftValue.text }
             ?.none { firstReferenceWithSameName?.isGoingAfter(it) ?: false } ?: true
-        if (firstReferenceWithSameName != null && isContainLocalVarSameName) {
+        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)
         }
     }


Test patch:
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..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
@@ -117,4 +117,37 @@ 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 blaBla: String
+                    |       get() = "bla".blaBla("bla")
+                    |   
+                    |   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()
+        )
+    }
 }