diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/StatementSwitchToExpressionSwitch.java b/core/src/main/java/com/google/errorprone/bugpatterns/StatementSwitchToExpressionSwitch.java index cbe93d3b8b6..20ec1b00bac 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/StatementSwitchToExpressionSwitch.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/StatementSwitchToExpressionSwitch.java @@ -544,7 +544,12 @@ private static SuggestedFix convertDirectlyToExpressionSwitch( String transformedBlockSource = transformBlock(caseTree, state, filteredStatements); if (firstCaseInGroup) { - groupedCaseCommentsAccumulator = new StringBuilder(); + groupedCaseCommentsAccumulator = + new StringBuilder( + caseIndex == 0 + ? extractCommentsBeforeFirstCase(switchTree, allSwitchComments).orElse("") + : ""); + replacementCodeBuilder.append("\n "); if (!isDefaultCase) { replacementCodeBuilder.append("case "); @@ -663,10 +668,10 @@ private static SuggestedFix convertToReturnSwitch( if (firstCaseInGroup) { groupedCaseCommentsAccumulator = - caseIndex == 0 - ? new StringBuilder( - extractCommentsBeforeFirstCase(switchTree, allSwitchComments).orElse("")) - : new StringBuilder(); + new StringBuilder( + caseIndex == 0 + ? extractCommentsBeforeFirstCase(switchTree, allSwitchComments).orElse("") + : ""); replacementCodeBuilder.append("\n "); if (!isDefaultCase) { @@ -831,7 +836,12 @@ private static SuggestedFix convertToAssignmentSwitch( transformAssignOrThrowBlock(caseTree, state, filteredStatements); if (firstCaseInGroup) { - groupedCaseCommentsAccumulator = new StringBuilder(); + groupedCaseCommentsAccumulator = + new StringBuilder( + caseIndex == 0 + ? extractCommentsBeforeFirstCase(switchTree, allSwitchComments).orElse("") + : ""); + replacementCodeBuilder.append("\n "); if (!isDefaultCase) { replacementCodeBuilder.append("case "); diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/StatementSwitchToExpressionSwitchTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/StatementSwitchToExpressionSwitchTest.java index 357b46452b4..92e3c2bb662 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/StatementSwitchToExpressionSwitchTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/StatementSwitchToExpressionSwitchTest.java @@ -154,6 +154,7 @@ public Test(int foo) {} public void foo(Side side) { // BUG: Diagnostic contains: [StatementSwitchToExpressionSwitch] switch (side) { + // Comment before first case case OBVERSE: // Explanatory comment System.out.println("this block cannot complete normally"); @@ -185,6 +186,7 @@ public Test(int foo) {} public void foo(Side side) { switch (side) { + // Comment before first case case OBVERSE: // Explanatory comment System.out.println("this block cannot complete normally"); @@ -211,6 +213,7 @@ public Test(int foo) {} public void foo(Side side) { switch (side) { case OBVERSE -> { + // Comment before first case // Explanatory comment System.out.println("this block cannot complete normally"); { @@ -3304,6 +3307,7 @@ public Test(int foo) { public int foo(Side side) { // BUG: Diagnostic contains: [StatementSwitchToExpressionSwitch] switch (side) { + /* Comment before first case */ case /* LHS comment */ HEART: // Inline comment x <<= 2; @@ -3347,6 +3351,7 @@ public Test(int foo) { public int foo(Side side) { switch (side) { + /* Comment before first case */ case /* LHS comment */ HEART: // Inline comment this.x <<= 2; @@ -3384,6 +3389,7 @@ public int foo(Side side) { this.x <<= switch (side) { case HEART -> + /* Comment before first case */ /* LHS comment */ // Inline comment 2;