Java: Don't extract method where it's not safe because of nullable result (IDEA-167391)

This commit is contained in:
Pavel Dolgov
2017-06-07 15:19:21 +03:00
parent 9ca8685340
commit a95807108c
5 changed files with 128 additions and 20 deletions

View File

@@ -0,0 +1,16 @@
class C {
private int[] list;
private Integer find(int id) {
<selection>
int n = 0;
for (int n1 : list) {
n = n1;
if (n == id) {
return n <= 0 ? null : n;
}
}
</selection>
throw new RuntimeException();
}
}

View File

@@ -0,0 +1,12 @@
class C {
private int[] list;
private int find(int id) {
<selection>for (int n : list) {
if (n == id) {
return n <= 0 ? 0 : n;
}
}</selection>
throw new RuntimeException();
}
}

View File

@@ -0,0 +1,21 @@
import org.jetbrains.annotations.Nullable;
class C {
private int[] list;
private int find(int id) {
Integer n = newMethod(id);
if (n != null) return n;
throw new RuntimeException();
}
@Nullable
private Integer newMethod(int id) {
for (int n : list) {
if (n == id) {
return n <= 0 ? 0 : n;
}
}
return null;
}
}

View File

@@ -95,6 +95,14 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
doTest();
}
public void testExitPoints10() throws Exception {
doExitPointsTest(false);
}
public void testExitPoints11() throws Exception {
doTest();
}
public void testNotNullCheckNameConflicts() throws Exception {
doTest();
}