From 142b1e7fd963190c16a52e9d771a26a8f90e7027 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Tue, 7 Jul 2020 15:17:24 +0700 Subject: [PATCH] Fix unwrap 'if' when it's immediately followed by label GitOrigin-RevId: 8dd9e0c5ba33fb2452e66126e51845ee1999af36 --- .../quickfix/SimplifyBooleanExpressionFix.java | 6 +++++- .../unwrapIfStatement/afterInsideSwitch2.java | 15 +++++++++++++++ .../unwrapIfStatement/beforeInsideSwitch2.java | 17 +++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/afterInsideSwitch2.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/beforeInsideSwitch2.java diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java index a433d1ddfd36..4f135ead41bb 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java @@ -344,7 +344,11 @@ public class SimplifyBooleanExpressionFix extends LocalQuickFixOnPsiElement { if (ifIndex >= 0 && ifIndex < siblingStatements.length - 1) { int labelIndex = ContainerUtil.indexOf(statements.subList(ifIndex, statements.size()), st -> st instanceof PsiSwitchLabelStatement); int limit = labelIndex != -1 ? labelIndex + ifIndex : siblingStatements.length; - parentBlock.deleteChildRange(siblingStatements[ifIndex + 1], siblingStatements[limit - 1]); + int startOffset = ifIndex + 1; + int endOffset = limit - 1; + if (startOffset <= endOffset) { + parentBlock.deleteChildRange(siblingStatements[startOffset], siblingStatements[endOffset]); + } } } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/afterInsideSwitch2.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/afterInsideSwitch2.java new file mode 100644 index 000000000000..633a71cd0621 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/afterInsideSwitch2.java @@ -0,0 +1,15 @@ +// "Unwrap 'if' statement" "true" +class X { + public String testUnwrap(String f) { + switch (f) { + case "A": + return "A"; + case "B": + return "b"; + case "D": + return "D"; + default: + return null; + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/beforeInsideSwitch2.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/beforeInsideSwitch2.java new file mode 100644 index 000000000000..59dd318c0da0 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/beforeInsideSwitch2.java @@ -0,0 +1,17 @@ +// "Unwrap 'if' statement" "true" +class X { + public String testUnwrap(String f) { + switch (f) { + case "A": + return "A"; + case "B": + if (true) { + return "b"; + } + case "D": + return "D"; + default: + return null; + } + } +} \ No newline at end of file