mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
do not skip control flow for unreachable code (IDEA-87654)
This commit is contained in:
@@ -46,7 +46,7 @@ public class ControlFlowWrapper {
|
||||
public ControlFlowWrapper(Project project, PsiElement codeFragment, PsiElement[] elements) throws PrepareFailedException {
|
||||
try {
|
||||
myControlFlow =
|
||||
ControlFlowFactory.getInstance(project).getControlFlow(codeFragment, new LocalsControlFlowPolicy(codeFragment), false, true);
|
||||
ControlFlowFactory.getInstance(project).getControlFlow(codeFragment, new LocalsControlFlowPolicy(codeFragment), false, false);
|
||||
}
|
||||
catch (AnalysisCanceledException e) {
|
||||
throw new PrepareFailedException(RefactoringBundle.message("extract.method.control.flow.analysis.failed"), e.getErrorElement());
|
||||
@@ -232,7 +232,7 @@ public class ControlFlowWrapper {
|
||||
return true;
|
||||
}
|
||||
|
||||
public List<PsiVariable> getInputVariables(final PsiElement codeFragment) {
|
||||
public List<PsiVariable> getInputVariables(final PsiElement codeFragment, PsiElement[] elements) {
|
||||
final List<PsiVariable> inputVariables = ControlFlowUtil.getInputVariables(myControlFlow, myFlowStart, myFlowEnd);
|
||||
List<PsiVariable> myInputVariables;
|
||||
if (myGenerateConditionalExit) {
|
||||
@@ -241,7 +241,17 @@ public class ControlFlowWrapper {
|
||||
myInputVariables = inputVariableList;
|
||||
}
|
||||
else {
|
||||
myInputVariables = inputVariables;
|
||||
List<PsiVariable> inputVariableList = new ArrayList<PsiVariable>(inputVariables);
|
||||
for (Iterator<PsiVariable> iterator = inputVariableList.iterator(); iterator.hasNext(); ) {
|
||||
PsiVariable variable = iterator.next();
|
||||
for (PsiElement element : elements) {
|
||||
if (PsiTreeUtil.isAncestor(element, variable, false)) {
|
||||
iterator.remove();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
myInputVariables = inputVariableList;
|
||||
}
|
||||
//varargs variables go last, otherwise order is induced by original ordering
|
||||
Collections.sort(myInputVariables, new Comparator<PsiVariable>() {
|
||||
|
||||
+1
-1
@@ -1073,7 +1073,7 @@ public class ExtractMethodProcessor implements MatchProvider {
|
||||
}
|
||||
|
||||
private boolean chooseTargetClass(PsiElement codeFragment, final Pass<ExtractMethodProcessor> extractPass) throws PrepareFailedException {
|
||||
final List<PsiVariable> inputVariables = myControlFlowWrapper.getInputVariables(codeFragment);
|
||||
final List<PsiVariable> inputVariables = myControlFlowWrapper.getInputVariables(codeFragment, myElements);
|
||||
|
||||
myNeedChangeContext = false;
|
||||
myTargetClass = myCodeFragmentMember instanceof PsiMember
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
class Test {
|
||||
|
||||
public void test(boolean b) {
|
||||
int a = 1;
|
||||
if (true) {
|
||||
System.out.println(a);
|
||||
} else {
|
||||
<selection>System.out.println(a);</selection>
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
class Test {
|
||||
|
||||
public void test(boolean b) {
|
||||
int a = 1;
|
||||
if (true) {
|
||||
newMethod(a);
|
||||
} else {
|
||||
newMethod(a);
|
||||
}
|
||||
}
|
||||
|
||||
private void newMethod(int a) {
|
||||
System.out.println(a);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+2
-1
@@ -1,6 +1,7 @@
|
||||
class Test {
|
||||
void foo() {
|
||||
final String str = newMethod();
|
||||
if (str == null) return;
|
||||
new Runnable() {
|
||||
public void run() {
|
||||
System.out.println(str);
|
||||
@@ -11,7 +12,7 @@ class Test {
|
||||
private String newMethod() {
|
||||
final String str = "";
|
||||
if (str == "") {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
@@ -393,6 +393,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
|
||||
public void testConstantConditionsAffectingControlFlow() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testConstantConditionsAffectingControlFlow1() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
public void testNotInitializedInsideFinally() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user