Elvis -> if intention: don't produce boilerplate code for return/break/continue/throw in RHS
#KT-14369 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
160de2dbe2
commit
2c424afefa
+16
@@ -78,6 +78,22 @@ class ElvisToIfThenIntention : SelfTargetingRangeIntention<KtBinaryExpression>(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!!
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo(s: String?) {
|
||||
while (true) {
|
||||
val t = s?.hashCode() ?:<caret> break
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
fun foo(s: String?) {
|
||||
while (true) {
|
||||
val t = s?.hashCode()
|
||||
if (t == null) break
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo(s: String?) {
|
||||
while (true) {
|
||||
val t = s?.hashCode() ?:<caret> continue
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
fun foo(s: String?) {
|
||||
while (true) {
|
||||
val t = s?.hashCode()
|
||||
if (t == null) continue
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(s: String?) {
|
||||
val t = s?.hashCode() ?:<caret> return
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun foo(s: String?) {
|
||||
val t = s?.hashCode()
|
||||
if (t == null) return
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// WITH_RUNTIME
|
||||
fun foo(s: String?) {
|
||||
val t = s?.hashCode() ?:<caret> throw Exception()
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// WITH_RUNTIME
|
||||
fun foo(s: String?) {
|
||||
val t = s?.hashCode()
|
||||
if (t == null) throw Exception()
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
class My(val x: Int)
|
||||
fun foo(arg: Any) {
|
||||
val y = (arg as? My)?.x <caret>?: return
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
class My(val x: Int)
|
||||
fun foo(arg: Any) {
|
||||
val y = if (arg is My) arg.x else return
|
||||
}
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user