[kotlin] disable if to elvis conversion when the call chain contains nullable types

^KTIJ-31327 fixed
^KTIJ-29999 fixed

GitOrigin-RevId: 06bab3f4df2debc2568104aef18d69bdbe10490b
This commit is contained in:
Anna Kozlova
2024-09-26 16:15:34 +00:00
committed by intellij-monorepo-bot
parent 6e7d163259
commit ee0d4019d2
5 changed files with 54 additions and 3 deletions
@@ -205,7 +205,7 @@ object IfThenTransformationUtils {
context(KaSession)
fun prepareIfThenToElvisInspectionData(element: KtIfExpression): IfThenToElvisInspectionData? {
val transformationData = IfThenTransformationUtils.buildTransformationData(element) ?: return null
val transformationData = buildTransformationData(element) ?: return null
val transformationStrategy = IfThenTransformationStrategy.create(transformationData) ?: return null
if (element.expressionType?.isUnitType != false) return null
@@ -234,7 +234,7 @@ object IfThenTransformationUtils {
private fun KaSession.clausesReplaceableByElvis(data: IfThenTransformationData): Boolean =
when {
data.negatedClause == null || data.negatedClause?.isNullOrBlockExpression() == true ->
data.negatedClause == null || data.negatedClause.isNullOrBlockExpression() == true ->
false
(data.negatedClause as? KtThrowExpression)?.let { throwsNullPointerExceptionWithNoArguments(it) } == true ->
false
@@ -245,7 +245,8 @@ object IfThenTransformationUtils {
data.baseClause.anyArgumentEvaluatesTo(data.checkedExpression) ->
true
hasImplicitReceiverReplaceableBySafeCall(data) || data.baseClause.hasFirstReceiverOf(data.checkedExpression) ->
data.baseClause.expressionType?.canBeNull == false
generateSequence(data.baseClause) { (it as? KtDotQualifiedExpression)?.receiverExpression }.toList()
.all { it.expressionType?.canBeNull == false || it.smartCastInfo?.smartCastType?.canBeNull == false }
else ->
false
}
@@ -742,6 +742,16 @@ public abstract class K2LocalInspectionTestGenerated extends AbstractK2LocalInsp
runTest("../../../idea/tests/testData/inspectionsLocal/branched/ifThenToElvis/nullCheckWithSelectorCallChain.kt");
}
@TestMetadata("nullabilityInReceiversOfBaseCause.kt")
public void testNullabilityInReceiversOfBaseCause() throws Exception {
runTest("../../../idea/tests/testData/inspectionsLocal/branched/ifThenToElvis/nullabilityInReceiversOfBaseCause.kt");
}
@TestMetadata("nullabilityInReceiversOfBaseCause1.kt")
public void testNullabilityInReceiversOfBaseCause1() throws Exception {
runTest("../../../idea/tests/testData/inspectionsLocal/branched/ifThenToElvis/nullabilityInReceiversOfBaseCause1.kt");
}
@TestMetadata("nullableResultWithImplicitReceiver.kt")
public void testNullableResultWithImplicitReceiver() throws Exception {
runTest("../../../idea/tests/testData/inspectionsLocal/branched/ifThenToElvis/nullableResultWithImplicitReceiver.kt");
@@ -453,6 +453,16 @@ public abstract class LocalInspectionTestGenerated extends AbstractLocalInspecti
runTest("testData/inspectionsLocal/branched/ifThenToElvis/nullCheckWithSelectorCallChain.kt");
}
@TestMetadata("nullabilityInReceiversOfBaseCause.kt")
public void testNullabilityInReceiversOfBaseCause() throws Exception {
runTest("testData/inspectionsLocal/branched/ifThenToElvis/nullabilityInReceiversOfBaseCause.kt");
}
@TestMetadata("nullabilityInReceiversOfBaseCause1.kt")
public void testNullabilityInReceiversOfBaseCause1() throws Exception {
runTest("testData/inspectionsLocal/branched/ifThenToElvis/nullabilityInReceiversOfBaseCause1.kt");
}
@TestMetadata("nullableResultWithImplicitReceiver.kt")
public void testNullableResultWithImplicitReceiver() throws Exception {
runTest("testData/inspectionsLocal/branched/ifThenToElvis/nullableResultWithImplicitReceiver.kt");
@@ -0,0 +1,13 @@
// WITH_STDLIB
// PROBLEM: none
data class Parent(
val child: Int?,
)
fun function(parent: Parent?) = if (parent =<caret>= null) {
"placeholder"
} else {
parent.child?.toString().orEmpty()
}
// IGNORE_K1
@@ -0,0 +1,17 @@
// PROBLEM: none
data class Sas(val f: Int?)
fun Int?.exampleFun() = when (this) {
null -> "a"
else -> "b"
}
fun getSas() : Sas? = Sas(null)
fun main() {
val sas = getSas()
val str = if (sa<caret>s == null) "null!!!" else sas.f.exampleFun()
println(str)
}
// IGNORE_K1