Skip to content

Commit

Permalink
WRONG_NAME_OF_VARIABLE_INSIDE_ACCESSOR false positive trigger
Browse files Browse the repository at this point in the history
### What's done:
* fixed false positive trigger
Closes #940
  • Loading branch information
Cheshiriks committed Jun 28, 2021
1 parent 2f96c88 commit b111e78
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,6 +36,11 @@ 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 funNames: List<String> = node
.parent(FILE)
?.findAllDescendantsWithSpecificType(FUN)
?.mapNotNull { it.findChildByType(IDENTIFIER) }
?.map { it.text } ?: emptyList()
val firstReferenceWithSameName = node
.findAllDescendantsWithSpecificType(REFERENCE_EXPRESSION)
.mapNotNull { it.findChildByType(IDENTIFIER) }
Expand All @@ -46,7 +54,7 @@ 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) {
if (firstReferenceWithSameName != null && isContainLocalVarSameName && funNames.all { it != firstReferenceWithSameName.text }) {
WRONG_NAME_OF_VARIABLE_INSIDE_ACCESSOR.warn(configRules, emitWarn, isFixMode, node.text, node.startOffset, node)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
}
}

0 comments on commit b111e78

Please sign in to comment.