mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-15 20:26:04 +07:00
IDEA-219189 "Split into 2 if's" generates redundant 'else if' clause
GitOrigin-RevId: 0218fd86bb09eea638be20dd96bd248ce498a399
This commit is contained in:
committed by
intellij-monorepo-bot
parent
fb0f9bb364
commit
1c84c9bef2
@@ -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}";
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
class C {
|
||||
void foo() {
|
||||
void foo(boolean a, boolean b, boolean c) {
|
||||
if (a) {
|
||||
if (b) {
|
||||
call();
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
class C {
|
||||
void foo() {
|
||||
void foo(boolean a, boolean b, boolean c) {
|
||||
if (a &<caret>& b) {
|
||||
call();
|
||||
} // foo
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
public class SplitCondition {
|
||||
void test(int x) {
|
||||
if(x > 0 &<caret>& x < 10) {}
|
||||
else if(x > 0 && x > 50)
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -77,12 +77,20 @@ public class SplitIfActionTest extends LightJavaCodeInsightTestCase {
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user