inline: do not delete call but warn when resulted expr is not a valid statement (IDEA-88886)

This commit is contained in:
anna
2012-07-16 19:20:51 +02:00
parent bea215be6b
commit 41932a9527
5 changed files with 54 additions and 4 deletions
@@ -190,6 +190,21 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor {
}
}
final PsiReturnStatement[] returnStatements = RefactoringUtil.findReturnStatements(myMethod);
for (PsiReturnStatement statement : returnStatements) {
PsiExpression value = statement.getReturnValue();
if (value != null && !(value instanceof PsiCallExpression)) {
for (UsageInfo info : usagesIn) {
PsiReference reference = info.getReference();
InlineUtil.TailCallType type = InlineUtil.getTailCallType(reference);
if (type == InlineUtil.TailCallType.Simple) {
conflicts.putValue(statement, "Inlined result won't be a valid statement");
break;
}
}
}
}
addInaccessibleMemberConflicts(myMethod, usagesIn, new ReferencedElementsCollector(), conflicts);
addInaccessibleSuperCallsConflicts(usagesIn, conflicts);
@@ -730,7 +745,7 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor {
if (returnValue == null) continue;
PsiStatement statement;
if (tailCallType == InlineUtil.TailCallType.Simple) {
if (returnValue instanceof PsiCallExpression) {
if (returnValue instanceof PsiExpression) {
PsiExpressionStatement exprStatement = (PsiExpressionStatement) myFactory.createStatementFromText("a;", null);
exprStatement.getExpression().replace(returnValue);
returnStatement.getParent().addBefore(exprStatement, returnStatement);
@@ -0,0 +1,26 @@
class AAA {
void fff(Project myProject) {
ensureFilesWritable(myProject, new String[1]);
}
private boolean ensu<caret>reFilesWritable(final Project project, final String... strings) {
return !ensureFilesWritable(strings).hasReadonlyFiles();
}
private Status ensureFilesWritable(final String[] strings) {
return new Status(strings);
}
class Status {
public Status(final String[] strings) {
//To change body of created methods use File | Settings | File Templates.
}
boolean hasReadonlyFiles() {
return true;
}
}
class Project {
}
}
@@ -7,6 +7,6 @@ class A {
}
public String method(boolean b) {
<caret>toInline(b);
return <caret>toInline(b);
}
}
@@ -2,7 +2,8 @@ class A {
public String method(boolean b) {
if (b) {
return;
return "b";
}
return "a";
}
}
@@ -185,12 +185,20 @@ public class InlineMethodTest extends LightRefactoringTestCase {
}
public void testMethodUsedInJavadoc() throws Exception {
doTestConflict("Inlined method is used in javadoc");
}
public void testNotAStatement() throws Exception {
doTestConflict("Inlined result won't be a valid statement");
}
private void doTestConflict(final String conflict) throws Exception {
try {
doTest();
fail("Conflict was not detected");
}
catch (BaseRefactoringProcessor.ConflictsInTestsException e) {
assertEquals("Inlined method is used in javadoc", e.getMessage());
assertEquals(conflict, e.getMessage());
}
}