Java control flow: Improved "Move return to computation" inspection, added more tests (IDEA-121153)

This commit is contained in:
Pavel Dolgov
2016-09-06 19:05:05 +03:00
parent a0afa83117
commit 21bc6ce944
23 changed files with 401 additions and 127 deletions
@@ -1,12 +0,0 @@
class T {
int f(int[] a, int b) {
int n = -1;
for (int i = 0; i < a.length; i++) {
if (a[i] == b) {
n = i;
break;
}
}
<warning descr="Return separated from computation of value of 'n'">return n;</warning>
}
}
@@ -1,7 +0,0 @@
class T {
int f(boolean b) {
int n = 0;
if (b) n = 1;
<warning descr="Return separated from computation of value of 'n'">return n;</warning>
}
}
@@ -1,21 +0,0 @@
class T {
String f() {
String r = "";
while (hasNext()) {
String s = next();
if (s != null) {
r = s;
break;
}
}
<warning descr="Return separated from computation of value of 'r'">return r;</warning>
}
boolean hasNext() {
return true;
}
String next() {
return null;
}
}
@@ -0,0 +1,12 @@
// "Move 'return' to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n = -1;
if (b) {
return 1;
}
else {
throw new RuntimeException();
}
}
}
@@ -4,6 +4,6 @@ class T {
int n = 0;
if (b) System.out.println("yes");
else return 2;
return n;
return 0;
}
}
@@ -4,6 +4,6 @@ class T {
int n = 0;
if (b) return 1;
else System.out.println("no");
return n;
return 0;
}
}
@@ -0,0 +1,12 @@
// "Move 'return' to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n = -1;
if (b) {
throw new RuntimeException();
}
else {
return 2;
}
}
}
@@ -0,0 +1,26 @@
// "Move 'return' to computation of the value of 'n'" "true"
class T {
int f(boolean b, boolean c) {
int n = -1;
if (b) {
try {
return g();
}
catch (RuntimeException e) {
d(e);
}
}
else {
return 2;
}
return n;
}
int g() {
return 1;
}
void d(Exception e) {
e.printStackTrace()
}
}
@@ -0,0 +1,12 @@
// "Move 'return' to computation of the value of 'n'" "true"
class T {
int f(int[] a, int b) {
int n = -1;
for (int i = 0; i < a.length; i++) {
if (a[i] == b) {
return i;
}
}
return -1;
}
}
@@ -0,0 +1,8 @@
// "Move 'return' to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n = 0;
if (b) return 1;
return 0;
}
}
@@ -0,0 +1,11 @@
// "Move 'return' to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n = g();
if (b) return 1;
return n;
}
int g() {
return 0;
}
}
@@ -0,0 +1,8 @@
// "Move 'return' to computation of the value of 'n'" "true"
class T {
int f(boolean b, int d) {
int n = d;
if (b) return 1;
return d;
}
}
@@ -0,0 +1,21 @@
// "Move 'return' to computation of the value of 'r'" "true"
class T {
String f() {
String r = "";
while (hasNext()) {
String s = next();
if (s != null) {
return s;
}
}
return "";
}
boolean hasNext() {
return true;
}
String next() {
return null;
}
}
@@ -0,0 +1,13 @@
// "Move 'return' to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n = -1;
if (b) {
n = 1;
}
else {
throw new RuntimeException();
}
r<caret>eturn n;
}
}
@@ -0,0 +1,10 @@
// "Move 'return' to computation of the value of 'n'" "false"
class T {
int f(boolean b) {
int n = -1;
if (b) {
throw new RuntimeException();
}
r<caret>eturn n;
}
}
@@ -0,0 +1,13 @@
// "Move 'return' to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n = -1;
if (b) {
throw new RuntimeException();
}
else {
n = 2;
}
r<caret>eturn n;
}
}
@@ -0,0 +1,26 @@
// "Move 'return' to computation of the value of 'n'" "true"
class T {
int f(boolean b, boolean c) {
int n = -1;
if (b) {
try {
n = g();
}
catch (RuntimeException e) {
d(e);
}
}
else {
n = 2;
}
r<caret>eturn n;
}
int g() {
return 1;
}
void d(Exception e) {
e.printStackTrace()
}
}
@@ -0,0 +1,13 @@
// "Move 'return' to computation of the value of 'n'" "true"
class T {
int f(int[] a, int b) {
int n = -1;
for (int i = 0; i < a.length; i++) {
if (a[i] == b) {
n = i;
break;
}
}
r<caret>eturn n;
}
}
@@ -0,0 +1,8 @@
// "Move 'return' to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n = 0;
if (b) n = 1;
re<caret>turn n;
}
}
@@ -0,0 +1,11 @@
// "Move 'return' to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n = g();
if (b) n = 1;
re<caret>turn n;
}
int g() {
return 0;
}
}
@@ -0,0 +1,8 @@
// "Move 'return' to computation of the value of 'n'" "true"
class T {
int f(boolean b, int d) {
int n = d;
if (b) n = 1;
re<caret>turn n;
}
}
@@ -0,0 +1,22 @@
// "Move 'return' to computation of the value of 'r'" "true"
class T {
String f() {
String r = "";
while (hasNext()) {
String s = next();
if (s != null) {
r = s;
break;
}
}
re<caret>turn r;
}
boolean hasNext() {
return true;
}
String next() {
return null;
}
}