[java] IDEA-263507 Line join silently changes semantics for nested ifs

GitOrigin-RevId: 59502372bb306221a601ee1aa0991a8092eb7335
This commit is contained in:
Tagir Valeev
2021-03-04 17:19:16 +07:00
committed by intellij-monorepo-bot
parent 08985be625
commit bac08ca4bc
4 changed files with 38 additions and 2 deletions

View File

@@ -56,8 +56,22 @@ public class BlockJoinLinesHandler implements JoinLinesHandlerDelegate {
if (foundStatement != null) return -1;
foundStatement = element;
}
if (foundStatement == null) return -1;
PsiElement parent = codeBlock.getParent();
if (foundStatement instanceof PsiIfStatement && parent instanceof PsiBlockStatement) {
PsiElement grandParent = parent.getParent();
if (grandParent instanceof PsiIfStatement &&
((PsiIfStatement)grandParent).getThenBranch() == parent &&
((PsiIfStatement)grandParent).getElseBranch() != null) {
/*
like "if(...) {if(...){...}} else {...}"
unwrapping the braces of outer 'if' then-branch will cause semantics change
*/
return -1;
}
}
try {
final PsiElement newStatement = codeBlock.getParent().replace(foundStatement);
final PsiElement newStatement = parent.replace(foundStatement);
return newStatement.getTextRange().getStartOffset();
}

View File

@@ -0,0 +1,11 @@
class C {
private static void fn(boolean condA, boolean condB) {
if (condA) {<caret>
if (condB) {
System.out.println("condA && condB");
}
} else {
System.out.println("!condA");
}
}
}

View File

@@ -0,0 +1,10 @@
class C {
private static void fn(boolean condA, boolean condB) {
if (condA) { if (condB) {
System.out.println("condA && condB");
}
} else {
System.out.println("!condA");
}
}
}

View File

@@ -195,7 +195,8 @@ public class JoinLinesTest extends LightJavaCodeInsightTestCase {
settings.IF_BRACE_FORCE = old;
}
}
public void testUnwrapCodeBlockIfElse() { doTest(); }
public void testAssignmentExpression() { doTest(); }
public void testAssignmentExpression2() { doTest(); }
public void testAssignmentExpressionPrecedence() { doTest(); }