Support BreakConverter in ConvertSwitchToIfIntention

Fixes IDEA-141261 'Replace 'switch' with 'if'' intention produces incorrect code if some 'case' clause contains 'break' statement inside 'if'
This commit is contained in:
Tagir Valeev
2018-09-26 11:58:20 +07:00
parent 7dd0cc4074
commit f859a4c439
13 changed files with 161 additions and 58 deletions
@@ -0,0 +1,15 @@
// "Replace 'switch' with 'if'" "true"
class X {
void m(String s, boolean r) {
if ("a".equals(s)) {
System.out.println("a");
if (r) {
return;
}
System.out.println("d");
} else {
System.out.println("d");
}
}
}
@@ -0,0 +1,14 @@
// "Replace 'switch' with 'if'" "true"
class X {
void m(String s, boolean r) {
if ("a".equals(s)) {
System.out.println("a");
if (r) {
} else {
throw new RuntimeException();
}
} else {
System.out.println("d");
}
}
}
@@ -0,0 +1,23 @@
// "Replace 'switch' with 'if'" "true"
class X {
int m(String s, int x) {
if (x > 0) {
SWITCH:
if ("a".equals(s)) {
System.out.println("a");
for (int i = 0; i < 10; i++) {
System.out.println(i);
if (i == x) return 0;
if (i == x * 2) break;
}
System.out.println("d");
} else {
System.out.println("d");
}
} else {
return 1;
}
return 0;
}
}
@@ -2,15 +2,13 @@
class X {
public void doSomething( String value) {
//comment1
//comment3
//comment4
//comment5
//comment6
//comment7
//comment8
if ("case1".equals(value)) {//comment2
} else if ("case2".equals(value)) {
} else {
//comment3
} else if ("case2".equals(value)) {//comment5
} else {//comment7
}
}
}
@@ -0,0 +1,14 @@
// "Replace 'switch' with 'if'" "true"
class X {
void m(String s, boolean r) {
swi<caret>tch (s) {
case "a":
System.out.println("a");
if (r) {
break;
}
default:
System.out.println("d");
}
}
}
@@ -0,0 +1,16 @@
// "Replace 'switch' with 'if'" "true"
class X {
void m(String s, boolean r) {
swi<caret>tch (s) {
case "a":
System.out.println("a");
if (r) {
break;
} else {
throw new RuntimeException();
}
default:
System.out.println("d");
}
}
}
@@ -0,0 +1,15 @@
// "Replace 'switch' with 'if'" "false"
class X {
void m(String s, boolean r) {
swi<caret>tch (s) {
case "a":
System.out.println("a");
if (r) {
break;
}
default:
System.out.println("d");
}
System.out.println("oops");
}
}
@@ -0,0 +1,22 @@
// "Replace 'switch' with 'if'" "true"
class X {
int m(String s, int x) {
if (x > 0) {
SWITCH:
swi<caret>tch (s){
case "a":
System.out.println("a");
for(int i=0; i<10; i++) {
System.out.println(i);
if(i == x) break SWITCH;
if(i == x*2) break;
}
default:
System.out.println("d");
}
} else {
return 1;
}
return 0;
}
}