Minor refactoring in DeprecatedCallChecker

This commit is contained in:
Alexander Udalov
2016-10-07 15:06:34 +03:00
parent 8949ffbef1
commit 9004367f3c
@@ -50,38 +50,37 @@ object DeprecatedCallChecker : CallChecker {
if (deprecation != null) { if (deprecation != null) {
trace.report(createDeprecationDiagnostic(element, deprecation)) trace.report(createDeprecationDiagnostic(element, deprecation))
} }
else if (targetDescriptor is PropertyDescriptor) { else if (targetDescriptor is PropertyDescriptor && shouldCheckPropertyGetter(element)) {
propertyGetterWorkaround(targetDescriptor, trace, element) targetDescriptor.getter?.let { check(it, trace, element) }
} }
} }
private val PROPERTY_SET_OPERATIONS = TokenSet.create(KtTokens.EQ, KtTokens.PLUSEQ, KtTokens.MINUSEQ, KtTokens.MULTEQ, private val PROPERTY_SET_OPERATIONS = TokenSet.create(*KtTokens.ALL_ASSIGNMENTS.types, KtTokens.PLUSPLUS, KtTokens.MINUSMINUS)
KtTokens.DIVEQ, KtTokens.PERCEQ, KtTokens.PLUSPLUS, KtTokens.MINUSMINUS)
private fun propertyGetterWorkaround(propertyDescriptor: PropertyDescriptor, trace: BindingTrace, expression: PsiElement) { private fun shouldCheckPropertyGetter(expression: PsiElement): Boolean {
// property getters do not come as callable yet, so we analyse surroundings to check for deprecation annotation on getter // property getters do not come as callable yet, so we analyse surroundings to check for deprecation annotation on getter
val binaryExpression = PsiTreeUtil.getParentOfType<KtBinaryExpression>(expression, KtBinaryExpression::class.java) val binaryExpression = PsiTreeUtil.getParentOfType<KtBinaryExpression>(expression, KtBinaryExpression::class.java)
if (binaryExpression != null) { if (binaryExpression != null) {
val left = binaryExpression.left val left = binaryExpression.left
if (left == expression && binaryExpression.operationToken in PROPERTY_SET_OPERATIONS) return if (left == expression && binaryExpression.operationToken in PROPERTY_SET_OPERATIONS) return false
val referenceExpressions = PsiTreeUtil.getChildrenOfType<KtReferenceExpression>(left, KtReferenceExpression::class.java) val referenceExpressions = PsiTreeUtil.getChildrenOfType<KtReferenceExpression>(left, KtReferenceExpression::class.java)
if (referenceExpressions != null) { if (referenceExpressions != null) {
for (expr in referenceExpressions) { for (expr in referenceExpressions) {
// skip binary set operations // skip binary set operations
if (expr == expression && binaryExpression.operationToken in PROPERTY_SET_OPERATIONS) return if (expr == expression && binaryExpression.operationToken in PROPERTY_SET_OPERATIONS) return false
} }
} }
} }
val unaryExpression = PsiTreeUtil.getParentOfType(expression, KtUnaryExpression::class.java) val unaryExpression = PsiTreeUtil.getParentOfType(expression, KtUnaryExpression::class.java)
// skip unary set operations // skip unary set operations
if (unaryExpression?.operationReference?.getReferencedNameElementType() in PROPERTY_SET_OPERATIONS) return if (unaryExpression?.operationReference?.getReferencedNameElementType() in PROPERTY_SET_OPERATIONS) return false
val callableExpression = PsiTreeUtil.getParentOfType(expression, KtCallableReferenceExpression::class.java) val callableExpression = PsiTreeUtil.getParentOfType(expression, KtCallableReferenceExpression::class.java)
// skip Type::property // skip Type::property
if (callableExpression != null && callableExpression.callableReference == expression) return if (callableExpression != null && callableExpression.callableReference == expression) return false
propertyDescriptor.getter?.let { check(it, trace, expression) } return true
} }
} }