Redundant Unit expression: simplify code a bit
This commit is contained in:
@@ -13,6 +13,7 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
|||||||
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.previousStatement
|
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.previousStatement
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.getParentOfTypesAndPredicate
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.lastBlockStatementOrThis
|
import org.jetbrains.kotlin.psi.psiUtil.lastBlockStatementOrThis
|
||||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
||||||
import org.jetbrains.kotlin.types.typeUtil.isUnit
|
import org.jetbrains.kotlin.types.typeUtil.isUnit
|
||||||
@@ -43,16 +44,14 @@ private fun KtReferenceExpression.isRedundantUnit(): Boolean {
|
|||||||
if (this == parent.lastBlockStatementOrThis()) {
|
if (this == parent.lastBlockStatementOrThis()) {
|
||||||
val prev = this.previousStatement() ?: return true
|
val prev = this.previousStatement() ?: return true
|
||||||
if (prev.isUnitLiteral()) return true
|
if (prev.isUnitLiteral()) return true
|
||||||
if (prev.getResolvedCall(analyze())?.resultingDescriptor?.returnType?.isUnit() == true) return true
|
val prevType = prev.getResolvedCall(analyze())?.resultingDescriptor?.returnType
|
||||||
if (prev is KtDeclaration) {
|
if (prevType != null) {
|
||||||
return if (prev is KtFunction)
|
return prevType.isUnit()
|
||||||
parent.parent?.parent?.let { it is KtIfExpression || it is KtWhenExpression } != true
|
|
||||||
else
|
|
||||||
true
|
|
||||||
}
|
}
|
||||||
return false
|
if (prev !is KtDeclaration) return false
|
||||||
|
if (prev !is KtFunction) return true
|
||||||
|
return parent.getParentOfTypesAndPredicate(true, KtIfExpression::class.java, KtWhenExpression::class.java) { true } == null
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
|||||||
Reference in New Issue
Block a user