ReplaceGuardClause inspection: do not report when 'if' expression is used as expression

This commit is contained in:
Toshiaki Kameyama
2019-07-23 00:27:28 +09:00
committed by Mikhail Glukhikh
parent a6690e4e35
commit 22c5e208a6
2 changed files with 8 additions and 1 deletions
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
@@ -58,9 +59,10 @@ class ReplaceGuardClauseWithFunctionCallInspection : AbstractApplicabilityBasedI
val valueArguments = call.valueArguments
if (valueArguments.size > 1) return false
if (calleeText != ILLEGAL_STATE_EXCEPTION && calleeText != ILLEGAL_ARGUMENT_EXCEPTION) return false
val context = call.analyze(BodyResolveMode.PARTIAL)
val context = call.analyze(BodyResolveMode.PARTIAL_WITH_CFA)
val argumentType = valueArguments.firstOrNull()?.getArgumentExpression()?.getType(context)
if (argumentType != null && !KotlinBuiltIns.isStringOrNullableString(argumentType)) return false
if (element.isUsedAsExpression(context)) return false
val fqName = call.getResolvedCall(context)?.resultingDescriptor?.fqNameSafe?.parent()
return fqName == FqName("kotlin.$calleeText") || fqName == FqName("java.lang.$calleeText")
}
@@ -8934,6 +8934,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
runTest("idea/testData/inspectionsLocal/replaceGuardClauseWithFunctionCall/notTargetException.kt");
}
@TestMetadata("usedAsExpression.kt")
public void testUsedAsExpression() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceGuardClauseWithFunctionCall/usedAsExpression.kt");
}
@TestMetadata("version12.kt")
public void testVersion12() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceGuardClauseWithFunctionCall/version12.kt");