Java: Don't offer "Unwrap 'else' branch" intention if it might cause unreachable code (IDEA-165428)

This commit is contained in:
Pavel Dolgov
2017-01-17 16:36:08 +03:00
parent 09aa5b41f8
commit ce236e3666
50 changed files with 594 additions and 87 deletions
@@ -0,0 +1,9 @@
// "Unwrap 'else' branch (changes semantics)" "true"
class T {
void f(boolean b) {
if (b)
System.out.println("When true");
System.out.println("Otherwise");
}
}
@@ -0,0 +1,12 @@
// "Unwrap 'else' branch (changes semantics)" "true"
class T {
void f(boolean b) {
if (b) {
System.out.println("When true");
}
// Before
System.out.println("Otherwise");
// After
}
}
@@ -0,0 +1,9 @@
// "Unwrap 'else' branch" "true"
class T {
void f(boolean b) {
if (b)
throw new RuntimeException("When true");
System.out.println("Otherwise");
}
}
@@ -0,0 +1,12 @@
// "Unwrap 'else' branch (changes semantics)" "true"
class T {
void f(boolean a, boolean b) {
if (a) {
if (b) {
System.out.println("When true");
}
System.out.println("Otherwise");
}
}
}
@@ -0,0 +1,13 @@
// "Unwrap 'else' branch" "true"
class T {
String f(boolean a, boolean b) {
if (a) {
if (b) {
return "When true";
}
System.out.println("Otherwise");
}
return "Default";
}
}
@@ -0,0 +1,13 @@
// "Unwrap 'else' branch" "true"
class T {
String f(boolean a, boolean b) {
if (a) {
if (b) {
return "When true";
}
return "Otherwise";
}
return "Default";
}
}
@@ -0,0 +1,15 @@
// "Unwrap 'else' branch (changes semantics)" "true"
class T {
void f(boolean a, boolean b) {
if (a) {
Label:
{
if (b)
System.out.println("When true");
throw new RuntimeException("Otherwise");
}
}
System.out.println("Done");
}
}
@@ -0,0 +1,12 @@
// "Unwrap 'else' branch (changes semantics)" "true"
class T {
void f(boolean a, boolean b) {
if (a) {
if (b)
System.out.println("When true");
System.out.println("Otherwise");
return;
}
}
}
@@ -0,0 +1,12 @@
// "Unwrap 'else' branch (changes semantics)" "true"
class T {
String f(boolean a, boolean b) {
if (a) {
if (b)
System.out.println("When true");
return "Otherwise";
}
return "Default";
}
}
@@ -0,0 +1,11 @@
// "Unwrap 'else' branch (changes semantics)" "true"
class T {
void f(boolean b) {
if (b)
System.out.println("When true");
// Before
System.out.println("Otherwise");
// After
}
}
@@ -0,0 +1,10 @@
// "Unwrap 'else' branch (changes semantics)" "true"
class T {
void f(boolean b) {
if (b) {
System.out.println("When true");
}
System.out.println("Otherwise");
}
}
@@ -0,0 +1,11 @@
// "Unwrap 'else' branch" "true"
class T {
String f(boolean b) {
if (b)
return "When true";
// Before
return "Otherwise";
// After
}
}
@@ -0,0 +1,11 @@
// "Unwrap 'else' branch (changes semantics)" "true"
class T {
String f(boolean b) {
if (b)
System.out.println("When true");
else
return "Otherwise";
return "Default";
}
}
@@ -0,0 +1,15 @@
// "Unwrap 'else' branch (changes semantics)" "true"
class T {
void f(boolean b, int n) {
switch (n) {
case 1:
if (b)
System.out.println("When true");
// Before
System.out.println("Otherwise");
// After
case 2:
}
}
}
@@ -0,0 +1,16 @@
// "Unwrap 'else' branch (changes semantics)" "true"
class T {
void f(boolean b, int n) {
switch (n) {
case 1:
if (b)
System.out.println("When true");
// Before
System.out.println("Otherwise");
// After
break;
case 2:
}
}
}
@@ -0,0 +1,17 @@
// "Unwrap 'else' branch (changes semantics)" "true"
class T {
void f(boolean b, int n) {
switch (n) {
case 1:
if (b)
System.out.println("When true");
// Before
System.out.println("Otherwise");
// After
return;
case 2:
}
System.out.println("Done");
}
}
@@ -0,0 +1,11 @@
// "Unwrap 'else' branch (changes semantics)" "true"
class T {
void f(Object o, boolean b) {
synchronized (o) {
if (b)
System.out.println("When true");
throw new RuntimeException("Otherwise");
}
}
}
@@ -0,0 +1,13 @@
// "Unwrap 'else' branch (changes semantics)" "true"
class T {
void f(boolean b) {
try {
if (b)
System.out.println("When true");
throw new RuntimeException("Otherwise");
} finally {
System.out.println("Finally");
}
}
}
@@ -0,0 +1,11 @@
// "Unwrap 'else' branch (changes semantics)" "true"
class T {
void f(boolean b) {
if (b)
System.out.println("When true");
<caret>else {
System.out.println("Otherwise");
}
}
}
@@ -0,0 +1,13 @@
// "Unwrap 'else' branch (changes semantics)" "true"
class T {
void f(boolean b) {
if (b) {
System.out.println("When true");
} <caret>else {
// Before
System.out.println("Otherwise");
// After
}
}
}
@@ -0,0 +1,11 @@
// "Unwrap 'else' branch" "true"
class T {
void f(boolean b) {
if (b)
throw new RuntimeException("When true");
<caret>else {
System.out.println("Otherwise");
}
}
}
@@ -0,0 +1,12 @@
// "Unwrap 'else' branch (changes semantics)" "true"
class T {
void f(boolean a, boolean b) {
if (a)
if (b) {
System.out.println("When true");
} <caret>else {
System.out.println("Otherwise");
}
}
}
@@ -0,0 +1,13 @@
// "Unwrap 'else' branch" "true"
class T {
String f(boolean a, boolean b) {
if (a)
if (b) {
return "When true";
} <caret>else {
System.out.println("Otherwise");
}
return "Default";
}
}
@@ -0,0 +1,13 @@
// "Unwrap 'else' branch" "true"
class T {
String f(boolean a, boolean b) {
if (a)
if (b) {
return "When true";
} <caret>else {
return "Otherwise";
}
return "Default";
}
}
@@ -0,0 +1,17 @@
// "Unwrap 'else' branch (changes semantics)" "true"
class T {
void f(boolean a, boolean b) {
if (a) {
Label:
{
if (b)
System.out.println("When true");
<caret>else {
throw new RuntimeException("Otherwise");
}
}
}
System.out.println("Done");
}
}
@@ -0,0 +1,15 @@
// "Unwrap 'else' branch (changes semantics)" "false"
class T {
void f(boolean b) {
Label:
{
if (b)
System.out.println("When true");
<caret>else {
throw new RuntimeException("Otherwise");
}
}
System.out.println("Done");
}
}
@@ -0,0 +1,14 @@
// "Unwrap 'else' branch (changes semantics)" "true"
class T {
void f(boolean a, boolean b) {
if (a) {
if (b)
System.out.println("When true");
<caret>else {
System.out.println("Otherwise");
return;
}
}
}
}
@@ -0,0 +1,13 @@
// "Unwrap 'else' branch (changes semantics)" "true"
class T {
String f(boolean a, boolean b) {
if (a) {
if (b)
System.out.println("When true");
<caret>else
return "Otherwise";
}
return "Default";
}
}
@@ -0,0 +1,14 @@
// "Unwrap 'else' branch (changes semantics)" "false"
class T {
void m(boolean b) {
if (b) {
System.out.println(1);
}
<caret>else {
System.out.println(2);
return;
}
System.out.println(3);
}
}
@@ -0,0 +1,14 @@
// "Unwrap 'else' branch" "false"
class T {
void m(boolean b) {
if (b) {
System.out.println(1);
}
<caret>else {
System.out.println(2);
return;
}
System.out.println(3);
}
}
@@ -0,0 +1,12 @@
// "Unwrap 'else' branch (changes semantics)" "true"
class T {
void f(boolean b) {
if (b)
System.out.println("When true");
<caret>else
// Before
System.out.println("Otherwise");
// After
}
}
@@ -0,0 +1,11 @@
// "Unwrap 'else' branch (changes semantics)" "true"
class T {
void f(boolean b) {
if (b) {
System.out.println("When true");
} <caret>else {
System.out.println("Otherwise");
}
}
}
@@ -0,0 +1,12 @@
// "Unwrap 'else' branch" "true"
class T {
String f(boolean b) {
if (b)
return "When true";
<caret>else
// Before
return "Otherwise";
// After
}
}
@@ -0,0 +1,16 @@
// "Unwrap 'else' branch (changes semantics)" "true"
class T {
void f(boolean b, int n) {
switch (n) {
case 1:
if (b)
System.out.println("When true");
<caret>else
// Before
System.out.println("Otherwise");
// After
case 2:
}
}
}
@@ -0,0 +1,18 @@
// "Unwrap 'else' branch (changes semantics)" "true"
class T {
void f(boolean b, int n) {
switch (n) {
case 1:
if (b)
System.out.println("When true");
<caret>else {
// Before
System.out.println("Otherwise");
// After
break;
}
case 2:
}
}
}
@@ -0,0 +1,19 @@
// "Unwrap 'else' branch (changes semantics)" "true"
class T {
void f(boolean b, int n) {
switch (n) {
case 1:
if (b)
System.out.println("When true");
<caret>else {
// Before
System.out.println("Otherwise");
// After
return;
}
case 2:
}
System.out.println("Done");
}
}
@@ -0,0 +1,18 @@
// "Unwrap 'else' branch (changes semantics)" "false"
class T {
void f(boolean b, int n) {
switch (n) {
case 1:
if (b)
System.out.println("When true");
<caret>else {
// Before
System.out.println("Otherwise");
// After
return;
}
}
System.out.println("Done");
}
}
@@ -0,0 +1,13 @@
// "Unwrap 'else' branch (changes semantics)" "true"
class T {
void f(Object o, boolean b) {
synchronized (o) {
if (b)
System.out.println("When true");
<caret>else {
throw new RuntimeException("Otherwise");
}
}
}
}
@@ -0,0 +1,14 @@
// "Unwrap 'else' branch (changes semantics)" "false"
class T {
void f(Object o, boolean b) {
synchronized (o) {
if (b)
System.out.println("When true");
<caret>else {
throw new RuntimeException("Otherwise");
}
}
System.out.println("Done");
}
}
@@ -0,0 +1,15 @@
// "Unwrap 'else' branch (changes semantics)" "true"
class T {
void f(boolean b) {
try {
if (b)
System.out.println("When true");
<caret>else {
throw new RuntimeException("Otherwise");
}
} finally {
System.out.println("Finally");
}
}
}
@@ -0,0 +1,15 @@
// "Unwrap 'else' branch (changes semantics)" "false"
class T {
void f(boolean b) {
try {
if (b)
System.out.println("When true");
<caret>else {
throw new RuntimeException("Otherwise");
}
} finally {
}
System.out.println("Done");
}
}