Merge SwitchStatementWithSingleDefaultInspection into SwitchStatementWithTooFewBranches

Fixes IDEA-199499 Improve "Replace 'switch' with 'if'" action and make it a quick-fix for "Switch has too few branches"
Now default is not counted as branch in SwitchStatementWithTooFewBranches
This commit is contained in:
Tagir Valeev
2018-09-26 17:01:22 +07:00
parent 22d6343b1e
commit 1b4e5c1e39
32 changed files with 454 additions and 284 deletions

View File

@@ -0,0 +1,13 @@
// "Replace 'switch' with 'if'" "true"
class X {
int m(String s, boolean r) {
if (r) return 1;
else if ("a".equals(s)) {
return 1;
} else if ("b".equals(s)) {
return 2;
} else {
return 3;
}
}
}

View File

@@ -0,0 +1,11 @@
// "Replace 'switch' with 'if'" "true"
class X {
int m(String s, boolean r) {
if ("a".equals(s)) {
return 1;
} else if ("b".equals(s)) {
return 2;
}
return 3;
}
}

View File

@@ -6,10 +6,7 @@ class X {
if (r) {
return;
}
System.out.println("d");
} else {
System.out.println("d");
}
System.out.println("d");
}
}

View File

@@ -3,16 +3,15 @@ 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;
{
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 {

View File

@@ -0,0 +1,9 @@
// "Replace 'switch' with 'if'" "true"
class X {
int m(String s, boolean r) {
//ignore
if ("x".equals(s)) {
System.out.println("foo");
}
}
}

View File

@@ -0,0 +1,11 @@
// "Replace 'switch' with 'if'" "true"
class X {
void m(String s, boolean r) {
if (r) {
if ("a".equals(s)) {
System.out.println("a");
}
System.out.println("d");
}
}
}

View File

@@ -4,11 +4,11 @@ class X {
//comment1
//comment4
//comment6
//comment7
//comment8
if ("case1".equals(value)) {//comment2
//comment3
} else if ("case2".equals(value)) {//comment5
} else {//comment7
}
}
}

View File

@@ -0,0 +1,12 @@
// "Replace 'switch' with 'if'" "true"
abstract class Test {
abstract Object getObject();
void foo() {
if (RuntimeException.class.equals(getObject().getClass())) {
System.out.println("RuntimeException");
} else {
System.out.println("Other");
}
}
}

View File

@@ -0,0 +1,15 @@
// "Replace 'switch' with 'if'" "true"
class X {
int m(String s, boolean r) {
if (r) return 1;
else
swi<caret>tch (s) {
case "a":
return 1;
case "b":
return 2;
default:
return 3;
}
}
}

View File

@@ -0,0 +1,13 @@
// "Replace 'switch' with 'if'" "true"
class X {
int m(String s, boolean r) {
swi<caret>tch (s) {
case "a":
return 1;
case "b":
return 2;
default:
return 3;
}
}
}

View File

@@ -0,0 +1,13 @@
// "Replace 'switch' with 'if'" "true"
class X {
int m(String s, boolean r) {
switc<caret>h(s) {
case "x":
System.out.println("foo");
break;
default: {
//ignore
}
}
}
}

View File

@@ -0,0 +1,12 @@
// "Replace 'switch' with 'if'" "true"
class X {
void m(String s, boolean r) {
if (r)
swi<caret>tch (s) {
case "a":
System.out.println("a");
default:
System.out.println("d");
}
}
}

View File

@@ -0,0 +1,15 @@
// "Replace 'switch' with 'if'" "true"
abstract class Test {
abstract Object getObject();
void foo() {
<caret>switch(getObject().getClass()) {
case RuntimeException.class:
System.out.println("RuntimeException");
break;
default:
System.out.println("Other");
break;
}
}
}