Support implicit receiver in if-then intentions #KT-16069 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
13a2612e20
commit
5df5a001a1
@@ -19,7 +19,7 @@ package org.jetbrains.kotlin.idea.intentions
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.evaluatesTo
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isStableVariable
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isStable
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator
|
||||
@@ -30,7 +30,7 @@ abstract class ReplaceSubstringIntention(text: String) : SelfTargetingRangeInten
|
||||
protected abstract fun applicabilityRangeInner(element: KtDotQualifiedExpression): TextRange?
|
||||
|
||||
override fun applicabilityRange(element: KtDotQualifiedExpression): TextRange? {
|
||||
if (element.receiverExpression.isStableVariable() && element.isMethodCall("kotlin.text.substring")) {
|
||||
if (element.receiverExpression.isStable() && element.isMethodCall("kotlin.text.substring")) {
|
||||
return applicabilityRangeInner(element)
|
||||
}
|
||||
return null
|
||||
|
||||
+11
-3
@@ -34,7 +34,9 @@ import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.BindingContextUtils
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
|
||||
import org.jetbrains.kotlin.resolve.calls.resolvedCallUtil.getImplicitReceiverValue
|
||||
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.types.TypeUtils
|
||||
@@ -140,7 +142,8 @@ fun KtPostfixExpression.inlineBaseExpressionIfApplicableWithPrompt(editor: Edito
|
||||
(this.baseExpression as? KtNameReferenceExpression)?.inlineIfDeclaredLocallyAndOnlyUsedOnceWithPrompt(editor)
|
||||
}
|
||||
|
||||
fun KtExpression.isStableVariable(context: BindingContext = this.analyze()): Boolean {
|
||||
fun KtExpression.isStable(context: BindingContext = this.analyze()): Boolean {
|
||||
if (this is KtConstantExpression || this is KtThisExpression) return true
|
||||
val descriptor = BindingContextUtils.extractVariableDescriptorFromReference(context, this)
|
||||
return descriptor is VariableDescriptor &&
|
||||
DataFlowValueFactory.isStableValue(descriptor, DescriptorUtils.getContainingModule(descriptor))
|
||||
@@ -168,11 +171,17 @@ data class IfThenToSelectData(
|
||||
(baseClause as KtDotQualifiedExpression).replaceFirstReceiver(
|
||||
factory, newReceiver!!, safeAccess = true)
|
||||
}
|
||||
else {
|
||||
else if (hasImplicitReceiver()) {
|
||||
factory.createExpressionByPattern("this?.$0", baseClause).insertSafeCalls(factory)
|
||||
} else {
|
||||
baseClause.insertSafeCalls(factory)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal fun hasImplicitReceiver(): Boolean {
|
||||
return baseClause.getResolvedCall(context)?.getImplicitReceiverValue() != null
|
||||
}
|
||||
}
|
||||
|
||||
internal fun KtIfExpression.buildSelectTransformationData(): IfThenToSelectData? {
|
||||
@@ -199,7 +208,6 @@ internal fun KtIfExpression.buildSelectTransformationData(): IfThenToSelectData?
|
||||
when (condition.isNegated) {
|
||||
true -> elseClause to thenClause
|
||||
false -> thenClause to elseClause
|
||||
else -> return null
|
||||
}
|
||||
}
|
||||
else -> return null
|
||||
|
||||
+2
-2
@@ -31,7 +31,7 @@ import org.jetbrains.kotlin.idea.intentions.SelfTargetingRangeIntention
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.convertToIfNotNullExpression
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.convertToIfNullExpression
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.introduceValueForCondition
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isStableVariable
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isStable
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.KtPostfixExpression
|
||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||
@@ -56,7 +56,7 @@ class DoubleBangToIfThenIntention : SelfTargetingRangeIntention<KtPostfixExpress
|
||||
val defaultException = KtPsiFactory(element).createExpression("throw NullPointerException()")
|
||||
|
||||
val isStatement = element.isUsedAsStatement(element.analyze())
|
||||
val isStable = base.isStableVariable()
|
||||
val isStable = base.isStable()
|
||||
|
||||
val ifStatement = if (isStatement)
|
||||
element.convertToIfNullExpression(base, defaultException)
|
||||
|
||||
+2
-2
@@ -22,7 +22,7 @@ import com.intellij.openapi.util.TextRange
|
||||
import org.jetbrains.kotlin.idea.intentions.SelfTargetingRangeIntention
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.convertToIfNotNullExpression
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.introduceValueForCondition
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isStableVariable
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isStable
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.KtBinaryExpression
|
||||
import org.jetbrains.kotlin.psi.KtPsiUtil
|
||||
@@ -39,7 +39,7 @@ class ElvisToIfThenIntention : SelfTargetingRangeIntention<KtBinaryExpression>(K
|
||||
val left = KtPsiUtil.safeDeparenthesize(element.left!!)
|
||||
val right = KtPsiUtil.safeDeparenthesize(element.right!!)
|
||||
|
||||
val leftIsStable = left.isStableVariable()
|
||||
val leftIsStable = left.isStable()
|
||||
|
||||
val ifStatement = element.convertToIfNotNullExpression(left, left, right)
|
||||
|
||||
|
||||
+1
-1
@@ -38,7 +38,7 @@ class IfThenToDoubleBangIntention : SelfTargetingRangeIntention<KtIfExpression>(
|
||||
|
||||
val matchesAsStatement = element.isUsedAsStatement(context) && (baseClause?.isNullExpressionOrEmptyBlock() ?: true)
|
||||
if (!matchesAsStatement &&
|
||||
!(baseClause?.evaluatesTo(receiverExpression) ?: false && receiverExpression.isStableVariable())) return null
|
||||
!(baseClause?.evaluatesTo(receiverExpression) ?: false && receiverExpression.isStable())) return null
|
||||
|
||||
var text = "Replace 'if' expression with '!!' expression"
|
||||
if (!throwExpression.throwsNullPointerExceptionWithNoArguments()) {
|
||||
|
||||
+4
-2
@@ -49,13 +49,15 @@ class IfThenToElvisIntention : SelfTargetingOffsetIndependentIntention<KtIfExpre
|
||||
private fun IfThenToSelectData.clausesReplaceableByElvis(): Boolean {
|
||||
if (baseClause == null || negatedClause == null || negatedClause.isNullOrBlockExpression()) return false
|
||||
if (negatedClause is KtThrowExpression && negatedClause.throwsNullPointerExceptionWithNoArguments()) return false
|
||||
return baseClause.evaluatesTo(receiverExpression) ||
|
||||
|
||||
return receiverExpression is KtThisExpression && hasImplicitReceiver() ||
|
||||
baseClause.evaluatesTo(receiverExpression) ||
|
||||
baseClause.hasFirstReceiverOf(receiverExpression) && !baseClause.hasNullableType(context)
|
||||
}
|
||||
|
||||
override fun isApplicableTo(element: KtIfExpression): Boolean {
|
||||
val ifThenToSelectData = element.buildSelectTransformationData() ?: return false
|
||||
if (!ifThenToSelectData.receiverExpression.isStableVariable(ifThenToSelectData.context)) return false
|
||||
if (!ifThenToSelectData.receiverExpression.isStable(ifThenToSelectData.context)) return false
|
||||
|
||||
val type = element.getType(ifThenToSelectData.context) ?: return false
|
||||
if (KotlinBuiltIns.isUnit(type)) return false
|
||||
|
||||
+3
-2
@@ -39,7 +39,7 @@ class IfThenToSafeAccessIntention : SelfTargetingOffsetIndependentIntention<KtIf
|
||||
|
||||
override fun isApplicableTo(element: KtIfExpression): Boolean {
|
||||
val ifThenToSelectData = element.buildSelectTransformationData() ?: return false
|
||||
if (!ifThenToSelectData.receiverExpression.isStableVariable(ifThenToSelectData.context)) return false
|
||||
if (!ifThenToSelectData.receiverExpression.isStable(ifThenToSelectData.context)) return false
|
||||
if (ifThenToSelectData.baseClause !is KtDotQualifiedExpression) {
|
||||
if (ifThenToSelectData.condition is KtIsExpression) {
|
||||
text = "Replace 'if' expression with safe cast expression"
|
||||
@@ -73,6 +73,7 @@ class IfThenToSafeAccessIntention : SelfTargetingOffsetIndependentIntention<KtIf
|
||||
baseClause == null -> false
|
||||
negatedClause == null && baseClause.isUsedAsExpression(context) -> false
|
||||
negatedClause != null && !negatedClause.isNullExpression() -> false
|
||||
else -> baseClause.evaluatesTo(receiverExpression) || baseClause.hasFirstReceiverOf(receiverExpression)
|
||||
else -> baseClause.evaluatesTo(receiverExpression) || baseClause.hasFirstReceiverOf(receiverExpression) ||
|
||||
receiverExpression is KtThisExpression && hasImplicitReceiver()
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -23,7 +23,7 @@ import com.intellij.psi.util.PsiTreeUtil
|
||||
import org.jetbrains.kotlin.idea.core.appendElement
|
||||
import org.jetbrains.kotlin.idea.core.replaced
|
||||
import org.jetbrains.kotlin.idea.intentions.SelfTargetingRangeIntention
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isStableVariable
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isStable
|
||||
import org.jetbrains.kotlin.idea.util.psi.patternMatching.matches
|
||||
import org.jetbrains.kotlin.idea.util.psi.patternMatching.toRange
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
@@ -36,7 +36,7 @@ class MergeWhenIntention : SelfTargetingRangeIntention<KtWhenExpression>(KtWhenE
|
||||
val subject1 = element.subjectExpression
|
||||
val subject2 = next.subjectExpression
|
||||
if (!subject1.matches(subject2)) return null
|
||||
if (subject1 != null && !subject1.isStableVariable()) return null
|
||||
if (subject1 != null && !subject1.isStable()) return null
|
||||
|
||||
val entries1 = element.entries
|
||||
val entries2 = next.entries
|
||||
|
||||
+2
-2
@@ -23,7 +23,7 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.intentions.SelfTargetingRangeIntention
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.convertToIfNotNullExpression
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.introduceValueForCondition
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isStableVariable
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isStable
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsStatement
|
||||
|
||||
@@ -37,7 +37,7 @@ class SafeAccessToIfThenIntention : SelfTargetingRangeIntention<KtSafeQualifiedE
|
||||
val receiver = KtPsiUtil.safeDeparenthesize(element.receiverExpression)
|
||||
val selector = element.selectorExpression!!
|
||||
|
||||
val receiverIsStable = receiver.isStableVariable()
|
||||
val receiverIsStable = receiver.isStable()
|
||||
|
||||
val psiFactory = KtPsiFactory(element)
|
||||
val dotQualified = psiFactory.createExpressionByPattern("$0.$1", receiver, selector)
|
||||
|
||||
@@ -23,17 +23,13 @@ import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
|
||||
import org.jetbrains.kotlin.idea.util.getResolutionScope
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isStable
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getLastParentOfTypeInRow
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getParentOfTypesAndPredicate
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
|
||||
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.types.typeUtil.isNullabilityMismatch
|
||||
|
||||
@@ -120,9 +116,3 @@ class SurroundWithNullCheckFix(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun KtExpression.isStable(context: BindingContext = this.analyze()): Boolean {
|
||||
val nullableType = this.getType(context) ?: return false
|
||||
val containingDescriptor = this.getResolutionScope(context, this.getResolutionFacade()).ownerDescriptor
|
||||
return DataFlowValueFactory.createDataFlowValue(this, nullableType, context, containingDescriptor).isStable
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
// WITH_RUNTIME
|
||||
fun String?.foo() = if (this == <caret>null) throw NullPointerException() else this
|
||||
@@ -0,0 +1,2 @@
|
||||
// WITH_RUNTIME
|
||||
fun String?.foo() = this!!
|
||||
@@ -0,0 +1,2 @@
|
||||
// WITH_RUNTIME
|
||||
fun String?.foo() = <caret>if (this == null) true else isEmpty()
|
||||
@@ -0,0 +1,2 @@
|
||||
// WITH_RUNTIME
|
||||
fun String?.foo() = this?.isEmpty() ?: true
|
||||
@@ -95,4 +95,12 @@
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?:'</problem_class>
|
||||
<description>Replace 'if' expression with elvis expression</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>implicitReceiver.kt</file>
|
||||
<line>2</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/implicitReceiver.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?:'</problem_class>
|
||||
<description>Replace 'if' expression with elvis expression</description>
|
||||
</problem>
|
||||
</problems>
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
// WITH_RUNTIME
|
||||
fun String?.foo() = <caret>if (this == null) null else isEmpty()
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
// WITH_RUNTIME
|
||||
fun String?.foo() = this?.isEmpty()
|
||||
+8
@@ -143,4 +143,12 @@
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
|
||||
<description>Remove redundant 'if' expression</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>implicitReceiver.kt</file>
|
||||
<line>2</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/implicitReceiver.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
|
||||
<description>Remove redundant 'if' expression</description>
|
||||
</problem>
|
||||
</problems>
|
||||
|
||||
@@ -1381,6 +1381,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("this.kt")
|
||||
public void testThis() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifThenToDoubleBang/this.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("throwByFqName.kt")
|
||||
public void testThrowByFqName() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifThenToDoubleBang/throwByFqName.kt");
|
||||
@@ -1510,6 +1516,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("implicitReceiver.kt")
|
||||
public void testImplicitReceiver() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifThenToElvis/implicitReceiver.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("isCheck.kt")
|
||||
public void testIsCheck() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifThenToElvis/isCheck.kt");
|
||||
@@ -1765,6 +1777,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("implicitReceiver.kt")
|
||||
public void testImplicitReceiver() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifThenToSafeAccess/implicitReceiver.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("isCheckSimple.kt")
|
||||
public void testIsCheckSimple() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifThenToSafeAccess/isCheckSimple.kt");
|
||||
|
||||
Reference in New Issue
Block a user