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 3 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,6 +7,7 @@ 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
Expand Down Expand Up @@ -46,7 +47,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 && firstReferenceWithSameName.treeParent.treeParent.elementType != CALL_EXPRESSION) {
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`() {
Cheshiriks marked this conversation as resolved.
Show resolved Hide resolved
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()
)
}
}