extract method: exclude return statements without value from occurrence analyzing; do not suggest artificial return type if return statements break control flow

This commit is contained in:
Anna Kozlova
2014-12-05 19:21:40 +01:00
parent 5dece3bd80
commit f7316391ce
4 changed files with 24 additions and 2 deletions
@@ -344,7 +344,7 @@ public class ExtractMethodProcessor implements MatchProvider {
@Nullable
private PsiVariable getArtificialOutputVariable() {
if (myOutputVariables.length == 0) {
if (myOutputVariables.length == 0 && myExitStatements.isEmpty()) {
if (myCanBeChainedConstructor) {
final Set<PsiField> fields = new HashSet<PsiField>();
for (PsiElement element : myElements) {
@@ -616,7 +616,7 @@ public class ExtractMethodProcessor implements MatchProvider {
final List<PsiStatement> filter = ContainerUtil.filter(myExitStatements, new Condition<PsiStatement>() {
@Override
public boolean value(PsiStatement statement) {
return statement instanceof PsiReturnStatement;
return statement instanceof PsiReturnStatement && ((PsiReturnStatement)statement).getReturnValue() != null;
}
});
final List<PsiExpression> map = ContainerUtil.map(filter, new Function<PsiStatement, PsiExpression>() {
@@ -0,0 +1,7 @@
class X {
void foo() {
<selection>if (true) return;
System.out.println();
</selection>
}
}
@@ -0,0 +1,11 @@
class X {
void foo() {
newMethod();
}
private int newMethod() {
if (true) return;
System.out.println();
}
}
@@ -666,6 +666,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
doTestReturnTypeChanged(PsiType.INT);
}
public void testNoReturnTypesSuggested() throws Exception {
doTestReturnTypeChanged(PsiType.INT);
}
public void testMultipleVarsInMethodNoReturnStatementAndAssignment() throws Exception {
//return type should not be suggested but still
doTestReturnTypeChanged(PsiType.INT);