mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-20 13:31:28 +07:00
[java] IDEA-263507 Line join silently changes semantics for nested ifs
GitOrigin-RevId: 59502372bb306221a601ee1aa0991a8092eb7335
This commit is contained in:
committed by
intellij-monorepo-bot
parent
08985be625
commit
bac08ca4bc
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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(); }
|
||||
|
||||
Reference in New Issue
Block a user