do not skip control flow for unreachable code (IDEA-87654)

This commit is contained in:
anna
2012-07-05 11:02:17 +02:00
parent 27da3854d3
commit 961086a6d1
6 changed files with 50 additions and 5 deletions
@@ -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>() {
@@ -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
@@ -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>
}
}
}
@@ -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);
}
}
@@ -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();
}