[kotlin] k2 extract function: don't introduce parameters for smart casts checks from inside selected fragment

- for unqualified access

^KTIJ-11836 fixed

GitOrigin-RevId: 40e1c08cf8f3442a357020cc1763476c0ed0ea78
This commit is contained in:
Anna Kozlova
2024-03-27 19:30:30 +00:00
committed by intellij-monorepo-bot
parent 404011423a
commit b1e3d43c0f
5 changed files with 47 additions and 17 deletions
@@ -2506,6 +2506,11 @@ public abstract class ExtractionTestGenerated extends AbstractExtractionTest {
runTest("testData/refactoring/extractFunction/parameters/misc/smartCastInside1.kt");
}
@TestMetadata("smartCastInsideUnqualified.kt")
public void testSmartCastInsideUnqualified() throws Exception {
runTest("testData/refactoring/extractFunction/parameters/misc/smartCastInsideUnqualified.kt");
}
@TestMetadata("thisInObject.kt")
public void testThisInObject() throws Exception {
runTest("testData/refactoring/extractFunction/parameters/misc/thisInObject.kt");
@@ -0,0 +1,7 @@
class A(val foo: Set<String>?) {
fun bar(maybeFoo: String) {
<selection>if (foo != null && foo.contains(maybeFoo)) println("not null")</selection>
}
}
// IGNORE_K1
@@ -0,0 +1,10 @@
class A(val foo: Set<String>?) {
fun bar(maybeFoo: String) {
__dummyTestFun__(maybeFoo)
}
private fun __dummyTestFun__(maybeFoo: String) {
if (foo != null && foo.contains(maybeFoo)) println("not null")
}
}
// IGNORE_K1
@@ -85,7 +85,6 @@ import org.jetbrains.kotlin.psi.psiUtil.isInsideOf
import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.types.expressions.OperatorConventions
import org.jetbrains.kotlin.idea.k2.refactoring.introduce.K2SemanticMatcher.isSemanticMatch
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedElementSelector
context(KtAnalysisSession)
internal fun ExtractionData.inferParametersInfo(
@@ -426,27 +425,31 @@ private fun ExtractionData.getBrokenReferencesInfo(body: KtBlockExpression): Lis
val parent = newRef.parent
val smartCast: KtType?
fun calculateSmartCastType(target: KtExpression): KtType? {
return analyze(target) {
val cast = target.getSmartCastInfo()?.smartCastType
when {
cast == null -> {
smartCastPossibleRoots.add(target)
null
}
//same qualified expressions without smartcast are present in the code fragment,
//so smart cast is done inside selection, no need to extract additional parameter
smartCastPossibleRoots.any { it.isSemanticMatch(target) } -> null
else -> cast
}
}
}
val possibleTypes: Set<KtType>
// Qualified property reference: a.b
val qualifiedExpression = newRef.getQualifiedExpressionForSelector()
if (qualifiedExpression != null) {
val smartCastTarget = originalRefExpr.parent as KtExpression
smartCast = analyze(smartCastTarget) {
val cast = smartCastTarget.getSmartCastInfo()?.smartCastType
when {
cast == null -> {
smartCastPossibleRoots.add(smartCastTarget)
null
}
//same qualified expressions without smartcast are present in the code fragment,
//so smart cast is done inside selection, no need to extract additional parameter
smartCastPossibleRoots.any { it.isSemanticMatch(smartCastTarget) } -> null
else -> cast
}
}
smartCast = calculateSmartCastType(smartCastTarget)
possibleTypes = analyze(smartCastTarget) { smartCastTarget.getExpectedType()?.let { setOf(it) } ?: emptySet() }
val (isCompanionObject, bothReceivers) = analyze(smartCastTarget) {
val symbol = originalRefExpr.resolveCall()?.singleCallOrNull<KtCallableMemberCall<*, *>>()?.partiallyAppliedSymbol
@@ -464,7 +467,7 @@ private fun ExtractionData.getBrokenReferencesInfo(body: KtBlockExpression): Lis
if (originalResolveResult.descriptor is KtProperty && parent is KtCallExpression && parent.calleeExpression == newRef && parent.getQualifiedExpressionForSelector() != null) {
continue
}
smartCast = analyze(originalRefExpr) { originalRefExpr.getSmartCastInfo()?.smartCastType }
smartCast = calculateSmartCastType(originalRefExpr)
possibleTypes = analyze(originalRefExpr) { originalRefExpr.getExpectedType()?.let { setOf(it) } ?: emptySet() }
}
@@ -1664,6 +1664,11 @@ public abstract class K2IntroduceFunctionTestGenerated extends AbstractK2Introdu
runTest("../../idea/tests/testData/refactoring/extractFunction/parameters/misc/smartCastInside1.kt");
}
@TestMetadata("smartCastInsideUnqualified.kt")
public void testSmartCastInsideUnqualified() throws Exception {
runTest("../../idea/tests/testData/refactoring/extractFunction/parameters/misc/smartCastInsideUnqualified.kt");
}
@TestMetadata("thisInObject.kt")
public void testThisInObject() throws Exception {
runTest("../../idea/tests/testData/refactoring/extractFunction/parameters/misc/thisInObject.kt");