Invert if condition intention: don't add unnecessary 'continue'

#KT-12329 Fixed
This commit is contained in:
Toshiaki Kameyama
2020-02-21 10:30:21 +01:00
committed by Vladimir Dolzhenko
parent 62e335ac88
commit e406669190
8 changed files with 75 additions and 1 deletions
@@ -184,12 +184,13 @@ class InvertIfConditionIntention : SelfTargetingIntention<KtIfExpression>(KtIfEx
}
if (thenBranch is KtBlockExpression) {
(thenBranch.statements.lastOrNull() as? KtContinueExpression)?.delete()
val range = thenBranch.contentRange()
if (!range.isEmpty) {
parent.addRangeAfter(range.first, range.last, ifExpression)
parent.addAfter(factory.createNewLine(), ifExpression)
}
} else {
} else if (thenBranch !is KtContinueExpression) {
parent.addAfter(thenBranch, ifExpression)
parent.addAfter(factory.createNewLine(), ifExpression)
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
fun test() {
for (x in "abc") {
<caret>if (x == 'a') continue
println("else")
}
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
fun test() {
for (x in "abc") {
if (x != 'a') {
println("else")
}
}
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
fun test() {
for (x in "abc") {
if (x != 'a') {
<caret>if (x == 'b') continue
println("else")
}
}
}
@@ -0,0 +1,10 @@
// WITH_RUNTIME
fun test() {
for (x in "abc") {
if (x != 'a') {
if (x != 'b') {
println("else")
}
}
}
}
@@ -0,0 +1,12 @@
// WITH_RUNTIME
fun test() {
for (x in "abc") {
if (x != 'a') {
<caret>if (x == 'b') {
println("foo")
continue
}
println("else")
}
}
}
@@ -0,0 +1,12 @@
// WITH_RUNTIME
fun test() {
for (x in "abc") {
if (x != 'a') {
if (x != 'b') {
println("else")
continue
}
println("foo")
}
}
}
@@ -9654,6 +9654,21 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/invertIfCondition/simple.kt");
}
@TestMetadata("unnecessaryContinue.kt")
public void testUnnecessaryContinue() throws Exception {
runTest("idea/testData/intentions/invertIfCondition/unnecessaryContinue.kt");
}
@TestMetadata("unnecessaryContinue2.kt")
public void testUnnecessaryContinue2() throws Exception {
runTest("idea/testData/intentions/invertIfCondition/unnecessaryContinue2.kt");
}
@TestMetadata("unnecessaryContinue3.kt")
public void testUnnecessaryContinue3() throws Exception {
runTest("idea/testData/intentions/invertIfCondition/unnecessaryContinue3.kt");
}
@TestMetadata("valueAndReturnBranches.kt")
public void testValueAndReturnBranches() throws Exception {
runTest("idea/testData/intentions/invertIfCondition/valueAndReturnBranches.kt");