Skip to content

Commit c438756

Browse files
markhbradyError Prone Team
authored and
Error Prone Team
committed
StatementSwitchToExpressionSwitch: for "assignment switch" and "direct conversion" transformations, retain comments appearing in the switch block before the first case
PiperOrigin-RevId: 696903291
1 parent ecfc4fc commit c438756

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

core/src/main/java/com/google/errorprone/bugpatterns/StatementSwitchToExpressionSwitch.java

+16-6
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,12 @@ private static SuggestedFix convertDirectlyToExpressionSwitch(
544544
String transformedBlockSource = transformBlock(caseTree, state, filteredStatements);
545545

546546
if (firstCaseInGroup) {
547-
groupedCaseCommentsAccumulator = new StringBuilder();
547+
groupedCaseCommentsAccumulator =
548+
new StringBuilder(
549+
caseIndex == 0
550+
? extractCommentsBeforeFirstCase(switchTree, allSwitchComments).orElse("")
551+
: "");
552+
548553
replacementCodeBuilder.append("\n ");
549554
if (!isDefaultCase) {
550555
replacementCodeBuilder.append("case ");
@@ -663,10 +668,10 @@ private static SuggestedFix convertToReturnSwitch(
663668

664669
if (firstCaseInGroup) {
665670
groupedCaseCommentsAccumulator =
666-
caseIndex == 0
667-
? new StringBuilder(
668-
extractCommentsBeforeFirstCase(switchTree, allSwitchComments).orElse(""))
669-
: new StringBuilder();
671+
new StringBuilder(
672+
caseIndex == 0
673+
? extractCommentsBeforeFirstCase(switchTree, allSwitchComments).orElse("")
674+
: "");
670675

671676
replacementCodeBuilder.append("\n ");
672677
if (!isDefaultCase) {
@@ -831,7 +836,12 @@ private static SuggestedFix convertToAssignmentSwitch(
831836
transformAssignOrThrowBlock(caseTree, state, filteredStatements);
832837

833838
if (firstCaseInGroup) {
834-
groupedCaseCommentsAccumulator = new StringBuilder();
839+
groupedCaseCommentsAccumulator =
840+
new StringBuilder(
841+
caseIndex == 0
842+
? extractCommentsBeforeFirstCase(switchTree, allSwitchComments).orElse("")
843+
: "");
844+
835845
replacementCodeBuilder.append("\n ");
836846
if (!isDefaultCase) {
837847
replacementCodeBuilder.append("case ");

core/src/test/java/com/google/errorprone/bugpatterns/StatementSwitchToExpressionSwitchTest.java

+6
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ public Test(int foo) {}
154154
public void foo(Side side) {
155155
// BUG: Diagnostic contains: [StatementSwitchToExpressionSwitch]
156156
switch (side) {
157+
// Comment before first case
157158
case OBVERSE:
158159
// Explanatory comment
159160
System.out.println("this block cannot complete normally");
@@ -185,6 +186,7 @@ public Test(int foo) {}
185186
186187
public void foo(Side side) {
187188
switch (side) {
189+
// Comment before first case
188190
case OBVERSE:
189191
// Explanatory comment
190192
System.out.println("this block cannot complete normally");
@@ -211,6 +213,7 @@ public Test(int foo) {}
211213
public void foo(Side side) {
212214
switch (side) {
213215
case OBVERSE -> {
216+
// Comment before first case
214217
// Explanatory comment
215218
System.out.println("this block cannot complete normally");
216219
{
@@ -3304,6 +3307,7 @@ public Test(int foo) {
33043307
public int foo(Side side) {
33053308
// BUG: Diagnostic contains: [StatementSwitchToExpressionSwitch]
33063309
switch (side) {
3310+
/* Comment before first case */
33073311
case /* LHS comment */ HEART:
33083312
// Inline comment
33093313
x <<= 2;
@@ -3347,6 +3351,7 @@ public Test(int foo) {
33473351
33483352
public int foo(Side side) {
33493353
switch (side) {
3354+
/* Comment before first case */
33503355
case /* LHS comment */ HEART:
33513356
// Inline comment
33523357
this.x <<= 2;
@@ -3384,6 +3389,7 @@ public int foo(Side side) {
33843389
this.x <<=
33853390
switch (side) {
33863391
case HEART ->
3392+
/* Comment before first case */
33873393
/* LHS comment */
33883394
// Inline comment
33893395
2;

0 commit comments

Comments
 (0)