ReplaceGuardClause inspection: don't report when argument is not String

This commit is contained in:
Toshiaki Kameyama
2019-06-10 18:46:59 +09:00
committed by Mikhail Glukhikh
parent 90b0ea73dc
commit 8cbcb66197
3 changed files with 22 additions and 2 deletions
@@ -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>(
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")
}
@@ -0,0 +1,8 @@
// PROBLEM: none
// WITH_RUNTIME
fun test() {
try {
} catch (e: Exception) {
<caret>if (e is RuntimeException) throw IllegalStateException(e)
}
}
@@ -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");