diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/afterEmptyElseBlock.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/afterEmptyElseBlock.java new file mode 100644 index 000000000000..f3233e8d2b47 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/afterEmptyElseBlock.java @@ -0,0 +1,15 @@ +// "Remove 'if' statement extracting side effects" "true" +import org.jetbrains.annotations.NotNull; +class X { + public String getRole(Object parent) { + if (parent instanceof Foo) { + ((Foo) parent).getBar(); + } + return null; + } +} + +interface Foo { + @NotNull + Integer getBar(); +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/beforeEmptyElseBlock.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/beforeEmptyElseBlock.java new file mode 100644 index 000000000000..003c3cb4a2ae --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/beforeEmptyElseBlock.java @@ -0,0 +1,17 @@ +// "Remove 'if' statement extracting side effects" "true" +import org.jetbrains.annotations.NotNull; +class X { + public String getRole(Object parent) { + if (parent instanceof Foo && ((Foo)parent).getBar() == null) { + return "a"; + } + else { + } + return null; + } +} + +interface Foo { + @NotNull + Integer getBar(); +} \ No newline at end of file diff --git a/platform/core-impl/src/com/intellij/psi/impl/source/codeStyle/CodeEditUtil.java b/platform/core-impl/src/com/intellij/psi/impl/source/codeStyle/CodeEditUtil.java index ad7a491ee3e3..ca7c9fc288ba 100644 --- a/platform/core-impl/src/com/intellij/psi/impl/source/codeStyle/CodeEditUtil.java +++ b/platform/core-impl/src/com/intellij/psi/impl/source/codeStyle/CodeEditUtil.java @@ -70,7 +70,7 @@ public class CodeEditUtil { ASTNode result = first; if (firstAddedLeaf != null) { ASTNode placeHolderEnd = makePlaceHolderBetweenTokens(prevLeaf, firstAddedLeaf, isFormattingRequired(prevLeaf, first), false); - if (placeHolderEnd != prevLeaf && first == firstAddedLeaf) { + if (placeHolderEnd != prevLeaf && first == firstAddedLeaf || result.getTreeParent() != parent) { result = placeHolderEnd; } ASTNode lastAddedLeaf = findLastLeaf(first, last);