Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WRONG_NAME_OF_VARIABLE_INSIDE_ACCESSOR false positive trigger #943

Merged
merged 4 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) }
Expand All @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`() {
Cheshiriks marked this conversation as resolved.
Show resolved Hide resolved
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()
)
}
}