Skip to content

Commit

Permalink
What's done:
Browse files Browse the repository at this point in the history
fixed false positive trigger
Closes #940
  • Loading branch information
Cheshiriks committed Jun 30, 2021
1 parent 9e849d5 commit d47fb3d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,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) }
Expand All @@ -47,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 && 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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
Expand Down

0 comments on commit d47fb3d

Please sign in to comment.