IDEA-219189 "Split into 2 if's" generates redundant 'else if' clause

GitOrigin-RevId: 0218fd86bb09eea638be20dd96bd248ce498a399
This commit is contained in:
Tagir Valeev
2019-08-05 07:44:33 +07:00
committed by intellij-monorepo-bot
parent fb0f9bb364
commit 1c84c9bef2
8 changed files with 63 additions and 4 deletions

View File

@@ -6,8 +6,10 @@ import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.siyeh.ig.psiutils.BoolUtils;
import com.siyeh.ig.psiutils.CommentTracker;
import com.siyeh.ig.psiutils.ControlFlowUtils;
import com.siyeh.ig.psiutils.EquivalenceChecker;
import com.siyeh.ipp.psiutils.ErrorUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -117,10 +119,21 @@ public class SplitConditionUtil {
if (nextIf == null) break;
PsiExpression nextCondition = PsiUtil.skipParenthesizedExprDown(nextIf.getCondition());
if (nextCondition == null) break;
if (PsiEquivalenceUtil.areElementsEquivalent(extract, nextCondition) && nextIf.getThenBranch() != null) {
EquivalenceChecker equivalence = EquivalenceChecker.getCanonicalPsiEquivalence();
if (nextIf.getThenBranch() == null) break;
if (equivalence.expressionsAreEquivalent(extract, nextCondition)) {
elseChain.add(tracker.text(nextIf.getThenBranch()));
chainFinished = true;
}
else if (nextIf.getElseBranch() == null && equivalence.expressionsAreEquivalent(
extract, factory.createExpressionFromText(BoolUtils.getNegatedExpressionText(nextCondition), nextCondition))) {
// skip duplicating else branch in cases like
// if(foo && bar) {1} else if (!foo) {2} =>
// if(foo) { if(bar) {1} } else {2}
elseBranch = nextIf.getThenBranch();
chainFinished = true;
break;
}
else {
if (!(nextCondition instanceof PsiPolyadicExpression)) break;
PsiPolyadicExpression nextPolyadic = (PsiPolyadicExpression)nextCondition;
@@ -150,6 +163,9 @@ public class SplitConditionUtil {
String thenString;
if (elseChain.isEmpty()) {
thenString = createIfString(leave, thenBranch, (String)null, tracker);
if (elseBranch != null) {
thenString = "{" + thenString + "}";
}
}
else {
thenString = "{" + createIfString(leave, thenBranch, String.join("\nelse ", elseChain), tracker) + "\n}";

View File

@@ -1,5 +1,5 @@
class C {
void foo() {
void foo(boolean a, boolean b, boolean c) {
if (a) {
if (b) {
call();

View File

@@ -0,0 +1,8 @@
public class SplitCondition {
void test(int x) {
if (x > 0) {
if (x < 10) {
} else if (x > 0 && x > 50)
} else if (x > 0 && x > 50)
}
}

View File

@@ -0,0 +1,11 @@
public class SplitCondition {
private static void appendString(String phrase) {
if (phrase != null) {
if (phrase.contains("abc")) {
System.out.println("abc!");
}
} else {
System.out.println("null");
}
}
}

View File

@@ -1,5 +1,5 @@
class C {
void foo() {
void foo(boolean a, boolean b, boolean c) {
if (a &<caret>& b) {
call();
} // foo

View File

@@ -0,0 +1,6 @@
public class SplitCondition {
void test(int x) {
if(x > 0 &<caret>& x < 10) {}
else if(x > 0 && x > 50)
}
}

View File

@@ -0,0 +1,10 @@
public class SplitCondition {
private static void appendString(String phrase) {
if (phrase != null &<caret>& phrase.contains("abc")) {
System.out.println("abc!");
}
else if (phrase == null) {
System.out.println("null");
}
}
}

View File

@@ -76,13 +76,21 @@ public class SplitIfActionTest extends LightJavaCodeInsightTestCase {
public void testWithoutSpaces() {
doTest();
}
public void testRedundantChainedCondition() {
doTest();
}
public void testIncomplete() {
configureByFile("/codeInsight/splitIfAction/before" + getTestName(false) + ".java");
SplitIfAction action = new SplitIfAction();
assertFalse(action.isAvailable(getProject(), getEditor(), getFile()));
}
public void testIncomplete2() {
doTest();
}
private void doTest() {
configureByFile("/codeInsight/splitIfAction/before" + getTestName(false)+ ".java");
CommonCodeStyleSettings settings = CodeStyle.getSettings(getFile()).getCommonSettings(JavaLanguage.INSTANCE);