ConvertToSingleReturn: default return value tuned; define it always before actual conversion

This commit is contained in:
Tagir Valeev
2019-04-12 17:19:54 +03:00
parent 7526618b79
commit e0d1fab43d
12 changed files with 109 additions and 29 deletions
@@ -1,12 +1,13 @@
// "Transform body to single exit-point form" "true"
class Test {
String test2(List<String> list, String foo, String bar) {
String result = foo;
String result = null;
boolean finished = false;
for (String s : list) {
for (int i = 0; i < 10; i++) {
bar = s;
if (s.length() == i) {
result = foo;
finished = true;
break;
}
@@ -1,9 +1,11 @@
// "Transform body to single exit-point form" "true"
class Test {
int test(String s) {
int result = 2;
int result = 1;
if (s == null) {
if (!(Math.random() > 0.5)) {
if (Math.random() > 0.5) {
result = 2;
} else {
result = 4;
}
} else {
@@ -11,7 +13,6 @@ class Test {
result = 3;
} else {
System.out.println(s);
result = 1;
}
}
return result;
@@ -0,0 +1,15 @@
// "Transform body to single exit-point form" "true"
import java.util.Collections;
class Test {
List<String> test(int x) {
List<String> result = Collections.emptyList();
if (x != 0) {
int rem = x % 3;
if (rem != 1) {
result = Collections.singletonList("foo");
}
}
return result;
}
}
@@ -1,9 +1,10 @@
// "Transform body to single exit-point form" "true"
class Test {
String test(int x) {
String result = "foo";
String result;
switch (x) {
case 1:
result = "foo";
break;
case 2:
result = "bar";
@@ -1,9 +1,11 @@
// "Transform body to single exit-point form" "true"
class Test {
String test(int x) {
String result = "foo";
String result;
synchronized (this) {
if (x != 0) {
if (x == 0) {
result = "foo";
} else {
if (x == 1) {
result = "bar";
} else {
@@ -1,10 +1,11 @@
// "Transform body to single exit-point form" "true"
class Test {
int test(String s) {
int result = -1;
int result;
try {
result = Integer.parseInt(s);
} catch (NumberFormatException ex) {
result = -1;
}
return result;
}
@@ -1,7 +1,7 @@
// "Transform body to single exit-point form" "true"
class Test {
int test(String s) {
int res = -1;
int res = -2;
boolean finished = false;
try {
res = Integer.parseInt(s);
@@ -9,12 +9,12 @@ class Test {
} catch (NumberFormatException ex) {
boolean result = s.isEmpty();
if (result) {
res = -1;
finished = true;
}
}
if (!finished) {
System.out.println("oops");
res = -2;
}
return res;
}
@@ -0,0 +1,18 @@
// "Transform body to single exit-point form" "true"
class Test {
int test(String s) {
int res = -2;
try {
res = Math.abs(Integer.parseInt(s));
} catch (NumberFormatException ex) {
boolean result = s.isEmpty();
if (result) {
res = -1;
}
}
if (res == -2) {
System.out.println("oops");
}
return res;
}
}
@@ -0,0 +1,11 @@
// "Transform body to single exit-point form" "true"
import java.util.Collections;
class Test {
List<String> <caret>test(int x) {
if (x == 0) return Collections.emptyList();
int rem = x % 3;
if (rem == 1) return Collections.emptyList();
return Collections.singletonList("foo");
}
}
@@ -0,0 +1,14 @@
// "Transform body to single exit-point form" "true"
class Test {
int <caret>test(String s) {
try {
return Math.abs(Integer.parseInt(s));
}
catch(NumberFormatException ex) {
boolean result = s.isEmpty();
if (result) return -1;
}
System.out.println("oops");
return -2;
}
}