ReplaceGuardClause inspection: do not report when 'if' expression is used as expression
This commit is contained in:
committed by
Mikhail Glukhikh
parent
a6690e4e35
commit
22c5e208a6
+3
-1
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.lexer.KtTokens
|
|||||||
import org.jetbrains.kotlin.name.FqName
|
import org.jetbrains.kotlin.name.FqName
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
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.getResolvedCall
|
||||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
|
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
|
||||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
||||||
@@ -58,9 +59,10 @@ class ReplaceGuardClauseWithFunctionCallInspection : AbstractApplicabilityBasedI
|
|||||||
val valueArguments = call.valueArguments
|
val valueArguments = call.valueArguments
|
||||||
if (valueArguments.size > 1) return false
|
if (valueArguments.size > 1) return false
|
||||||
if (calleeText != ILLEGAL_STATE_EXCEPTION && calleeText != ILLEGAL_ARGUMENT_EXCEPTION) 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)
|
val argumentType = valueArguments.firstOrNull()?.getArgumentExpression()?.getType(context)
|
||||||
if (argumentType != null && !KotlinBuiltIns.isStringOrNullableString(argumentType)) return false
|
if (argumentType != null && !KotlinBuiltIns.isStringOrNullableString(argumentType)) return false
|
||||||
|
if (element.isUsedAsExpression(context)) return false
|
||||||
val fqName = call.getResolvedCall(context)?.resultingDescriptor?.fqNameSafe?.parent()
|
val fqName = call.getResolvedCall(context)?.resultingDescriptor?.fqNameSafe?.parent()
|
||||||
return fqName == FqName("kotlin.$calleeText") || fqName == FqName("java.lang.$calleeText")
|
return fqName == FqName("kotlin.$calleeText") || fqName == FqName("java.lang.$calleeText")
|
||||||
}
|
}
|
||||||
|
|||||||
+5
@@ -8934,6 +8934,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
|||||||
runTest("idea/testData/inspectionsLocal/replaceGuardClauseWithFunctionCall/notTargetException.kt");
|
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")
|
@TestMetadata("version12.kt")
|
||||||
public void testVersion12() throws Exception {
|
public void testVersion12() throws Exception {
|
||||||
runTest("idea/testData/inspectionsLocal/replaceGuardClauseWithFunctionCall/version12.kt");
|
runTest("idea/testData/inspectionsLocal/replaceGuardClauseWithFunctionCall/version12.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user