Refactoring: Unnecessary variable inspection
This commit is contained in:
committed by
Mikhail Glukhikh
parent
52053695fe
commit
844dd1c43c
@@ -67,30 +67,29 @@ class UnnecessaryVariableInspection : AbstractKotlinInspection() {
|
|||||||
val enclosingElement = KtPsiUtil.getEnclosingElementForLocalDeclaration(property) ?: return null
|
val enclosingElement = KtPsiUtil.getEnclosingElementForLocalDeclaration(property) ?: return null
|
||||||
val initializer = property.initializer ?: return null
|
val initializer = property.initializer ?: return null
|
||||||
|
|
||||||
if (!property.isVar && initializer is KtNameReferenceExpression && property.typeReference == null) {
|
fun isExactCopy(): Boolean {
|
||||||
val context = property.analyze()
|
if (!property.isVar && initializer is KtNameReferenceExpression && property.typeReference == null) {
|
||||||
val initializerDescriptor = context[REFERENCE_TARGET, initializer]
|
val context = property.analyze()
|
||||||
if (initializerDescriptor is VariableDescriptor) {
|
val initializerDescriptor = context[REFERENCE_TARGET, initializer] as? VariableDescriptor ?: return false
|
||||||
if (!initializerDescriptor.isVar && initializerDescriptor.containingDeclaration is FunctionDescriptor) {
|
if (initializerDescriptor.isVar) return false
|
||||||
if (ReferencesSearch.search(property, LocalSearchScope(enclosingElement)).findFirst() != null) {
|
if (initializerDescriptor.containingDeclaration !is FunctionDescriptor) return false
|
||||||
return Status.EXACT_COPY
|
return ReferencesSearch.search(property, LocalSearchScope(enclosingElement)).findFirst() != null
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
val nextStatement = property.getNextSiblingIgnoringWhitespaceAndComments()
|
fun isReturnOnly(): Boolean {
|
||||||
if (nextStatement is KtReturnExpression) {
|
val nextStatement = property.getNextSiblingIgnoringWhitespaceAndComments() as? KtReturnExpression ?: return false
|
||||||
val returned = nextStatement.returnedExpression
|
val returned = nextStatement.returnedExpression as? KtNameReferenceExpression ?: return false
|
||||||
if (returned is KtNameReferenceExpression) {
|
val context = nextStatement.analyze()
|
||||||
val context = nextStatement.analyze()
|
return context[REFERENCE_TARGET, returned] == context[DECLARATION_TO_DESCRIPTOR, property]
|
||||||
if (context[REFERENCE_TARGET, returned] == context[DECLARATION_TO_DESCRIPTOR, property]) {
|
|
||||||
return Status.RETURN_ONLY
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null
|
return when {
|
||||||
|
isExactCopy() -> Status.EXACT_COPY
|
||||||
|
isReturnOnly() -> Status.RETURN_ONLY
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isActiveFor(property: KtProperty) = statusFor(property) != null
|
fun isActiveFor(property: KtProperty) = statusFor(property) != null
|
||||||
|
|||||||
Reference in New Issue
Block a user