Property accessor isn't recursive when called on another object

So #KT-18120 Fixed
This commit is contained in:
Dmitry Neverov
2017-06-04 12:25:34 +03:00
committed by Mikhail Glukhikh
parent ecfc2236a6
commit e2aa3518df
3 changed files with 29 additions and 0 deletions
@@ -20,6 +20,8 @@ import com.intellij.codeInspection.*
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
@@ -96,6 +98,9 @@ class RecursivePropertyAccessorInspection : AbstractKotlinInspection() {
val bindingContext = element.analyze()
val target = bindingContext[REFERENCE_TARGET, element]
if (target != bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, propertyAccessor.property]) return false
(element.parent as? KtQualifiedExpression)?.let {
if (it.receiverExpression.text != KtTokens.THIS_KEYWORD.value && !it.hasObjectReceiver(bindingContext)) return false
}
return isSameAccessor(element, propertyAccessor.isGetter)
}
@@ -114,5 +119,10 @@ class RecursivePropertyAccessorInspection : AbstractKotlinInspection() {
namedFunctionDescriptor != syntheticDescriptor.setMethod) return false
return isSameAccessor(element, isGetter)
}
private fun KtQualifiedExpression.hasObjectReceiver(context: BindingContext) : Boolean {
val receiver = receiverExpression as? KtReferenceExpression ?: return false
return (context[REFERENCE_TARGET, receiver] as? ClassDescriptor)?.kind == ClassKind.OBJECT
}
}
}
@@ -116,4 +116,13 @@
<description>Recursive property accessor</description>
</problem>
<problem>
<file>recursivePropertyAccessors.kt</file>
<line>45</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/recursivePropertyAccessors.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Recursive property accessor</problem_class>
<description>Recursive property accessor</description>
</problem>
</problems>
@@ -38,4 +38,14 @@ class A {
A.g = 99
}
}
}
object Obj {
val s: String
get() = Obj.s
}
class Node(val next: Node?) {
val last: Node
get() = if (next != null) next.last else this
}