IDEA-211549 Inline method: support negation at the call site

GitOrigin-RevId: d9afdc05a45b153ce23bf5f6e091606f43b050f6
This commit is contained in:
Tagir Valeev
2019-04-28 15:00:48 +03:00
committed by intellij-monorepo-bot
parent 2961bb5130
commit 364d5bc833
4 changed files with 46 additions and 2 deletions
@@ -327,10 +327,16 @@ public class InlineUtil {
if (element instanceof PsiMethodReferenceExpression) return TailCallType.Return;
PsiExpression methodCall = PsiTreeUtil.getParentOfType(element, PsiMethodCallExpression.class);
if (methodCall == null) return TailCallType.None;
PsiElement callParent = methodCall.getParent();
PsiElement callParent = PsiUtil.skipParenthesizedExprUp(methodCall.getParent());
if (callParent instanceof PsiReturnStatement || callParent instanceof PsiLambdaExpression) {
return TailCallType.Return;
}
if (callParent instanceof PsiExpression && BoolUtils.isNegation((PsiExpression)callParent)) {
PsiElement negationParent = PsiUtil.skipParenthesizedExprUp(callParent.getParent());
if (negationParent instanceof PsiReturnStatement || negationParent instanceof PsiLambdaExpression) {
return TailCallType.Invert;
}
}
if (callParent instanceof PsiExpressionStatement) {
PsiStatement curElement = (PsiStatement)callParent;
while (true) {
@@ -555,7 +561,17 @@ public class InlineUtil {
Continue((methodCopy, callSite, returnType) -> {
extractReturnValues(methodCopy, true);
return null;
}),
}),
Invert((methodCopy, callSite, returnType) -> {
for (PsiReturnStatement statement : PsiUtil.findReturnStatements(methodCopy)) {
PsiExpression value = statement.getReturnValue();
if (value != null) {
CommentTracker ct = new CommentTracker();
ct.replaceAndRestoreComments(value, BoolUtils.getNegatedExpressionText(value, ct));
}
}
return null;
}),
Return((methodCopy, callSite, returnType) -> null);
@Nullable
@@ -0,0 +1,13 @@
class Test {
boolean check(String s) {
if (s == null) return false;
s = s.trim();
if (s.isEmpty()) return false;
return s.length() % 2 == 0;
}
boolean use(String s) {
return !<caret>check(s + s);
}
}
@@ -0,0 +1,11 @@
class Test {
boolean use(String s) {
String s1 = s + s;
if (s1 == null) return true;
s1 = s1.trim();
if (s1.isEmpty()) return true;
return s1.length() % 2 != 0;
}
}
@@ -514,6 +514,10 @@ public class InlineMethodTest extends LightRefactoringTestCase {
public void testBooleanModelFinalCondition() {
doTestAssertBadReturn();
}
public void testInvertMethod() {
doTest();
}
@Override
protected Sdk getProjectJDK() {