diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/ElvisToIfThenIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/ElvisToIfThenIntention.kt index 7948cc5eff1..f8897b6b44f 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/ElvisToIfThenIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/ElvisToIfThenIntention.kt @@ -78,6 +78,22 @@ class ElvisToIfThenIntention : SelfTargetingRangeIntention(K val right = KtPsiUtil.safeDeparenthesize(element.right!!) val leftSafeCastReceiver = left.findSafeCastReceiver(context) + if (leftSafeCastReceiver == null) { + val property = (KtPsiUtil.safeDeparenthesize(element).parent as? KtProperty) + val propertyName = property?.name + if ((right is KtReturnExpression || right is KtBreakExpression || right is KtContinueExpression || right is KtThrowExpression) + && propertyName != null + ) { + val parent = property.parent + val factory = KtPsiFactory(element) + factory.createExpressionByPattern("if ($0 == null) $1", propertyName, right) + parent.addAfter(factory.createExpressionByPattern("if ($0 == null) $1", propertyName, right), property) + parent.addAfter(factory.createNewLine(), property) + element.replace(left) + return + } + } + val (leftIsStable, ifStatement) = if (leftSafeCastReceiver != null) { val newReceiver = leftSafeCastReceiver.left val typeReference = leftSafeCastReceiver.right!! diff --git a/idea/testData/intentions/branched/elvisToIfThen/assignmentAndBreak.kt b/idea/testData/intentions/branched/elvisToIfThen/assignmentAndBreak.kt new file mode 100644 index 00000000000..3adb0bb0fac --- /dev/null +++ b/idea/testData/intentions/branched/elvisToIfThen/assignmentAndBreak.kt @@ -0,0 +1,5 @@ +fun foo(s: String?) { + while (true) { + val t = s?.hashCode() ?: break + } +} diff --git a/idea/testData/intentions/branched/elvisToIfThen/assignmentAndBreak.kt.after b/idea/testData/intentions/branched/elvisToIfThen/assignmentAndBreak.kt.after new file mode 100644 index 00000000000..d0ceb8fc8b8 --- /dev/null +++ b/idea/testData/intentions/branched/elvisToIfThen/assignmentAndBreak.kt.after @@ -0,0 +1,6 @@ +fun foo(s: String?) { + while (true) { + val t = s?.hashCode() + if (t == null) break + } +} diff --git a/idea/testData/intentions/branched/elvisToIfThen/assignmentAndContinue.kt b/idea/testData/intentions/branched/elvisToIfThen/assignmentAndContinue.kt new file mode 100644 index 00000000000..a5140d083da --- /dev/null +++ b/idea/testData/intentions/branched/elvisToIfThen/assignmentAndContinue.kt @@ -0,0 +1,5 @@ +fun foo(s: String?) { + while (true) { + val t = s?.hashCode() ?: continue + } +} \ No newline at end of file diff --git a/idea/testData/intentions/branched/elvisToIfThen/assignmentAndContinue.kt.after b/idea/testData/intentions/branched/elvisToIfThen/assignmentAndContinue.kt.after new file mode 100644 index 00000000000..95c27901742 --- /dev/null +++ b/idea/testData/intentions/branched/elvisToIfThen/assignmentAndContinue.kt.after @@ -0,0 +1,6 @@ +fun foo(s: String?) { + while (true) { + val t = s?.hashCode() + if (t == null) continue + } +} \ No newline at end of file diff --git a/idea/testData/intentions/branched/elvisToIfThen/assignmentAndReturn.kt b/idea/testData/intentions/branched/elvisToIfThen/assignmentAndReturn.kt new file mode 100644 index 00000000000..6cbc2e18d51 --- /dev/null +++ b/idea/testData/intentions/branched/elvisToIfThen/assignmentAndReturn.kt @@ -0,0 +1,3 @@ +fun foo(s: String?) { + val t = s?.hashCode() ?: return +} \ No newline at end of file diff --git a/idea/testData/intentions/branched/elvisToIfThen/assignmentAndReturn.kt.after b/idea/testData/intentions/branched/elvisToIfThen/assignmentAndReturn.kt.after new file mode 100644 index 00000000000..dcaf24baafb --- /dev/null +++ b/idea/testData/intentions/branched/elvisToIfThen/assignmentAndReturn.kt.after @@ -0,0 +1,4 @@ +fun foo(s: String?) { + val t = s?.hashCode() + if (t == null) return +} \ No newline at end of file diff --git a/idea/testData/intentions/branched/elvisToIfThen/assignmentAndThrow.kt b/idea/testData/intentions/branched/elvisToIfThen/assignmentAndThrow.kt new file mode 100644 index 00000000000..b1d1880ee31 --- /dev/null +++ b/idea/testData/intentions/branched/elvisToIfThen/assignmentAndThrow.kt @@ -0,0 +1,4 @@ +// WITH_RUNTIME +fun foo(s: String?) { + val t = s?.hashCode() ?: throw Exception() +} diff --git a/idea/testData/intentions/branched/elvisToIfThen/assignmentAndThrow.kt.after b/idea/testData/intentions/branched/elvisToIfThen/assignmentAndThrow.kt.after new file mode 100644 index 00000000000..ef13c68293b --- /dev/null +++ b/idea/testData/intentions/branched/elvisToIfThen/assignmentAndThrow.kt.after @@ -0,0 +1,5 @@ +// WITH_RUNTIME +fun foo(s: String?) { + val t = s?.hashCode() + if (t == null) throw Exception() +} diff --git a/idea/testData/intentions/branched/elvisToIfThen/safeCastAndReturn.kt b/idea/testData/intentions/branched/elvisToIfThen/safeCastAndReturn.kt new file mode 100644 index 00000000000..c707a178654 --- /dev/null +++ b/idea/testData/intentions/branched/elvisToIfThen/safeCastAndReturn.kt @@ -0,0 +1,4 @@ +class My(val x: Int) +fun foo(arg: Any) { + val y = (arg as? My)?.x ?: return +} \ No newline at end of file diff --git a/idea/testData/intentions/branched/elvisToIfThen/safeCastAndReturn.kt.after b/idea/testData/intentions/branched/elvisToIfThen/safeCastAndReturn.kt.after new file mode 100644 index 00000000000..416f6e51b5b --- /dev/null +++ b/idea/testData/intentions/branched/elvisToIfThen/safeCastAndReturn.kt.after @@ -0,0 +1,4 @@ +class My(val x: Int) +fun foo(arg: Any) { + val y = if (arg is My) arg.x else return +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index c677e94b0c7..f54cc22194e 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -2165,6 +2165,26 @@ public class IntentionTestGenerated extends AbstractIntentionTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/branched/elvisToIfThen"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true); } + @TestMetadata("assignmentAndBreak.kt") + public void testAssignmentAndBreak() throws Exception { + runTest("idea/testData/intentions/branched/elvisToIfThen/assignmentAndBreak.kt"); + } + + @TestMetadata("assignmentAndContinue.kt") + public void testAssignmentAndContinue() throws Exception { + runTest("idea/testData/intentions/branched/elvisToIfThen/assignmentAndContinue.kt"); + } + + @TestMetadata("assignmentAndReturn.kt") + public void testAssignmentAndReturn() throws Exception { + runTest("idea/testData/intentions/branched/elvisToIfThen/assignmentAndReturn.kt"); + } + + @TestMetadata("assignmentAndThrow.kt") + public void testAssignmentAndThrow() throws Exception { + runTest("idea/testData/intentions/branched/elvisToIfThen/assignmentAndThrow.kt"); + } + @TestMetadata("callExpression.kt") public void testCallExpression() throws Exception { runTest("idea/testData/intentions/branched/elvisToIfThen/callExpression.kt"); @@ -2205,6 +2225,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/branched/elvisToIfThen/safeCast.kt"); } + @TestMetadata("safeCastAndReturn.kt") + public void testSafeCastAndReturn() throws Exception { + runTest("idea/testData/intentions/branched/elvisToIfThen/safeCastAndReturn.kt"); + } + @TestMetadata("safeCastUnstable.kt") public void testSafeCastUnstable() throws Exception { runTest("idea/testData/intentions/branched/elvisToIfThen/safeCastUnstable.kt");