RedundantUnitExpressionInspection: make isRedundantUnit public

This commit is contained in:
Dmitry Gridin
2020-06-19 19:51:15 +07:00
parent 360a5bf348
commit 4ac7dc0744
@@ -1,5 +1,5 @@
/* /*
* Copyright 2000-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. * Copyright 2000-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/ */
@@ -27,45 +27,52 @@ import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.typeUtil.isUnit import org.jetbrains.kotlin.types.typeUtil.isUnit
class RedundantUnitExpressionInspection : AbstractKotlinInspection(), CleanupLocalInspectionTool { class RedundantUnitExpressionInspection : AbstractKotlinInspection(), CleanupLocalInspectionTool {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor { override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor = referenceExpressionVisitor(fun(expression) {
return referenceExpressionVisitor(fun(expression) { if (isRedundantUnit(expression)) {
if (expression.isRedundantUnit()) { holder.registerProblem(
holder.registerProblem( expression,
expression, KotlinBundle.message("redundant.unit"),
KotlinBundle.message("redundant.unit"), ProblemHighlightType.LIKE_UNUSED_SYMBOL,
ProblemHighlightType.LIKE_UNUSED_SYMBOL, RemoveRedundantUnitFix()
RemoveRedundantUnitFix() )
) }
} })
})
} companion object {
} fun isRedundantUnit(referenceExpression: KtReferenceExpression): Boolean {
if (!referenceExpression.isUnitLiteral()) return false
private fun KtReferenceExpression.isRedundantUnit(): Boolean { val parent = referenceExpression.parent ?: return false
if (!isUnitLiteral()) return false if (parent is KtReturnExpression) {
val parent = this.parent ?: return false val expectedReturnType = parent.expectedReturnType() ?: return false
if (parent is KtReturnExpression) { return expectedReturnType.nameIfStandardType != KotlinBuiltIns.FQ_NAMES.any.shortName() && !expectedReturnType.isMarkedNullable
val expectedReturnType = parent.expectedReturnType() ?: return false }
return expectedReturnType.nameIfStandardType != KotlinBuiltIns.FQ_NAMES.any.shortName() && !expectedReturnType.isMarkedNullable
} if (parent is KtBlockExpression) {
if (parent is KtBlockExpression) { // Do not report just 'Unit' in function literals (return@label Unit is OK even in literals)
// Do not report just 'Unit' in function literals (return@label Unit is OK even in literals) if (parent.getParentOfType<KtFunctionLiteral>(strict = true) != null) return false
if (parent.getParentOfType<KtFunctionLiteral>(strict = true) != null) return false
if (referenceExpression == parent.lastBlockStatementOrThis()) {
if (this == parent.lastBlockStatementOrThis()) { val prev = referenceExpression.previousStatement() ?: return true
val prev = this.previousStatement() ?: return true if (prev.isUnitLiteral()) return true
if (prev.isUnitLiteral()) return true val prevType = prev.resolveToCall(BodyResolveMode.FULL)?.resultingDescriptor?.returnType
val prevType = prev.resolveToCall(BodyResolveMode.FULL)?.resultingDescriptor?.returnType if (prevType != null) {
if (prevType != null) { return prevType.isUnit()
return prevType.isUnit() }
} if (prev !is KtDeclaration) return false
if (prev !is KtDeclaration) return false if (prev !is KtFunction) return true
if (prev !is KtFunction) return true return parent.getParentOfTypesAndPredicate(
return parent.getParentOfTypesAndPredicate(true, KtIfExpression::class.java, KtWhenExpression::class.java) { true } == null true,
KtIfExpression::class.java,
KtWhenExpression::class.java
) { true } == null
}
return true
}
return false
} }
return true
} }
return false
} }
private fun KtExpression.isUnitLiteral(): Boolean = private fun KtExpression.isUnitLiteral(): Boolean =