CollapseIntoLoopAction: avoid counting loop for two iterations when step is not +1/-1

GitOrigin-RevId: d8aef85c6d2620a9dd7e9216932388672a049979
This commit is contained in:
Tagir Valeev
2020-06-17 11:01:23 +07:00
committed by intellij-monorepo-bot
parent 613d4368f1
commit 868aa78cf3
5 changed files with 33 additions and 0 deletions

View File

@@ -136,6 +136,8 @@ public class CollapseIntoLoopAction implements IntentionAction {
last = cur;
}
if (start == null || step == null) return null;
// Prefer for(int x : new int[] {12, 17}) over for(int x = 12; x <= 17; x+= 5)
if (myLoopElements.size() == 2 && step != 1L && step != -1L) return null;
String suffix = PsiType.LONG.equals(myType) ? "L" : "";
String initial = myType.getCanonicalText() + " " + varName + "=" + start + suffix;
String condition =

View File

@@ -0,0 +1,8 @@
// "Collapse into loop" "true"
class X {
void test() {
for (int i = 12; i <= 22; i += 5) {
System.out.println(i);
}
}
}

View File

@@ -0,0 +1,8 @@
// "Collapse into loop" "true"
class X {
void test() {
for (int i : new int[]{12, 17}) {
System.out.println(i);
}
}
}

View File

@@ -0,0 +1,8 @@
// "Collapse into loop" "true"
class X {
void test() {
<caret>System.out.println(12);
System.out.println(17);
System.out.println(22);
}
}

View File

@@ -0,0 +1,7 @@
// "Collapse into loop" "true"
class X {
void test() {
<caret>System.out.println(12);
System.out.println(17);
}
}