diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceGuardClauseWithFunctionCallInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceGuardClauseWithFunctionCallInspection.kt index 2f3e2bd25d3..7e0f55c9951 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceGuardClauseWithFunctionCallInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceGuardClauseWithFunctionCallInspection.kt @@ -7,7 +7,8 @@ package org.jetbrains.kotlin.idea.inspections import com.intellij.openapi.editor.Editor import com.intellij.openapi.project.Project -import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall +import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.core.ShortenReferences import org.jetbrains.kotlin.idea.core.replaced import org.jetbrains.kotlin.idea.intentions.callExpression @@ -15,7 +16,10 @@ import org.jetbrains.kotlin.idea.util.CommentSaver import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall +import org.jetbrains.kotlin.resolve.calls.callUtil.getType import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe +import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode class ReplaceGuardClauseWithFunctionCallInspection : AbstractApplicabilityBasedInspection( KtIfExpression::class.java @@ -46,7 +50,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 fqName = call.resolveToCall()?.resultingDescriptor?.fqNameSafe?.parent() + val context = call.analyze(BodyResolveMode.PARTIAL) + val argumentType = valueArguments.firstOrNull()?.getArgumentExpression()?.getType(context) + if (argumentType != null && !KotlinBuiltIns.isStringOrNullableString(argumentType)) return false + val fqName = call.getResolvedCall(context)?.resultingDescriptor?.fqNameSafe?.parent() return fqName == FqName("kotlin.$calleeText") || fqName == FqName("java.lang.$calleeText") } diff --git a/idea/testData/inspectionsLocal/replaceGuardClauseWithFunctionCall/notStringArgument.kt b/idea/testData/inspectionsLocal/replaceGuardClauseWithFunctionCall/notStringArgument.kt new file mode 100644 index 00000000000..fc712a646cb --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceGuardClauseWithFunctionCall/notStringArgument.kt @@ -0,0 +1,8 @@ +// PROBLEM: none +// WITH_RUNTIME +fun test() { + try { + } catch (e: Exception) { + if (e is RuntimeException) throw IllegalStateException(e) + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index c59481f4902..4c7057389a1 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -8914,6 +8914,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/replaceGuardClauseWithFunctionCall"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true); } + @TestMetadata("notStringArgument.kt") + public void testNotStringArgument() throws Exception { + runTest("idea/testData/inspectionsLocal/replaceGuardClauseWithFunctionCall/notStringArgument.kt"); + } + @TestMetadata("notTargetException.kt") public void testNotTargetException() throws Exception { runTest("idea/testData/inspectionsLocal/replaceGuardClauseWithFunctionCall/notTargetException.kt");