Elvis -> if intention: don't introduce new variable when RHS type is Nothing
This commit is contained in:
committed by
Dmitry Gridin
parent
f9f8fd055b
commit
752f6d8f72
+8
-3
@@ -19,8 +19,10 @@ import org.jetbrains.kotlin.lexer.KtTokens
|
|||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.resolve.BindingContext
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
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.lazy.BodyResolveMode
|
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||||
import org.jetbrains.kotlin.types.TypeUtils
|
import org.jetbrains.kotlin.types.TypeUtils
|
||||||
|
import org.jetbrains.kotlin.types.typeUtil.isNothing
|
||||||
|
|
||||||
class ElvisToIfThenIntention : SelfTargetingRangeIntention<KtBinaryExpression>(
|
class ElvisToIfThenIntention : SelfTargetingRangeIntention<KtBinaryExpression>(
|
||||||
KtBinaryExpression::class.java,
|
KtBinaryExpression::class.java,
|
||||||
@@ -71,9 +73,12 @@ class ElvisToIfThenIntention : SelfTargetingRangeIntention<KtBinaryExpression>(
|
|||||||
if (leftSafeCastReceiver == null) {
|
if (leftSafeCastReceiver == null) {
|
||||||
val property = (KtPsiUtil.safeDeparenthesize(element).parent as? KtProperty)
|
val property = (KtPsiUtil.safeDeparenthesize(element).parent as? KtProperty)
|
||||||
val propertyName = property?.name
|
val propertyName = property?.name
|
||||||
if ((right is KtReturnExpression || right is KtBreakExpression || right is KtContinueExpression || right is KtThrowExpression)
|
val rightIsReturnOrJumps = right is KtReturnExpression
|
||||||
&& propertyName != null
|
|| right is KtBreakExpression
|
||||||
) {
|
|| right is KtContinueExpression
|
||||||
|
|| right is KtThrowExpression
|
||||||
|
|| right.getType(context)?.isNothing() == true
|
||||||
|
if (rightIsReturnOrJumps && propertyName != null) {
|
||||||
val parent = property.parent
|
val parent = property.parent
|
||||||
val factory = KtPsiFactory(element)
|
val factory = KtPsiFactory(element)
|
||||||
factory.createExpressionByPattern("if ($0 == null) $1", propertyName, right)
|
factory.createExpressionByPattern("if ($0 == null) $1", propertyName, right)
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
fun foo(list: List<String>): String {
|
||||||
|
val first = list.firstOrNull() ?:<caret> error("empty")
|
||||||
|
return "First length: ${first.length}"
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
fun foo(list: List<String>): String {
|
||||||
|
val first = list.firstOrNull()
|
||||||
|
if (first == null) error("empty")
|
||||||
|
return "First length: ${first.length}"
|
||||||
|
}
|
||||||
@@ -2279,6 +2279,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
runTest("idea/testData/intentions/branched/elvisToIfThen/assignmentAndContinue.kt");
|
runTest("idea/testData/intentions/branched/elvisToIfThen/assignmentAndContinue.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("assignmentAndError.kt")
|
||||||
|
public void testAssignmentAndError() throws Exception {
|
||||||
|
runTest("idea/testData/intentions/branched/elvisToIfThen/assignmentAndError.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("assignmentAndReturn.kt")
|
@TestMetadata("assignmentAndReturn.kt")
|
||||||
public void testAssignmentAndReturn() throws Exception {
|
public void testAssignmentAndReturn() throws Exception {
|
||||||
runTest("idea/testData/intentions/branched/elvisToIfThen/assignmentAndReturn.kt");
|
runTest("idea/testData/intentions/branched/elvisToIfThen/assignmentAndReturn.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user