ConvertSwitchToIfIntention: fixes after review IDEA-CR-37311:

1. Do not suggest intention if `default` case is not the last one and fallthrough is possible into or out of that case
2. Generate a code-block if side-effect should be extracted to variable
This commit is contained in:
Tagir Valeev
2018-09-27 10:17:10 +07:00
parent cf83ab3242
commit 625c1cc676
9 changed files with 118 additions and 18 deletions
@@ -0,0 +1,15 @@
// "Replace 'switch' with 'if'" "true"
class X {
int m(int i) {
if (i > 0) {
int i1 = ++i;
if (i1 == 1) {
System.out.println(1);
System.out.println(2);
} else if (i1 == 2) {
System.out.println(2);
}
}
}
}
@@ -0,0 +1,12 @@
// "Replace 'switch' with 'if'" "true"
class X {
void m(String s) {
if ("foo".equals(s)) {
System.out.println(1);
} else if ("bar".equals(s)) {
System.out.println(3);
} else {
System.out.println(2);
}
}
}
@@ -0,0 +1,12 @@
// "Replace 'switch' with 'if'" "true"
class X {
int m(int i) {
if (i > 0)
sw<caret>itch (++i) {
case 1:
System.out.println(1);
case 2:
System.out.println(2);
}
}
}
@@ -0,0 +1,15 @@
// "Replace 'switch' with 'if'" "true"
class X {
void m(String s) {
swi<caret>tch (s) {
case "foo":
System.out.println(1);
break;
default:
System.out.println(2);
break;
case "bar":
System.out.println(3);
}
}
}
@@ -0,0 +1,15 @@
// "Replace 'switch' with 'if'" "false"
class X {
void m(String s) {
swi<caret>tch (s) {
case "foo":
System.out.println(1);
default:
System.out.println(2);
break;
case "bar":
System.out.println(3);
break;
}
}
}
@@ -0,0 +1,15 @@
// "Replace 'switch' with 'if'" "false"
class X {
void m(String s) {
swi<caret>tch (s) {
case "foo":
System.out.println(1);
break;
default:
System.out.println(2);
case "bar":
System.out.println(3);
break;
}
}
}