Skip to content

Commit

Permalink
StatementSwitchToExpressionSwitch: for "assignment switch" and "direc…
Browse files Browse the repository at this point in the history
…t conversion" transformations, retain comments appearing in the `switch` block before the first `case`

PiperOrigin-RevId: 696903291
  • Loading branch information
markhbrady authored and Error Prone Team committed Nov 15, 2024
1 parent ecfc4fc commit c438756
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 ");
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand All @@ -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");
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -3384,6 +3389,7 @@ public int foo(Side side) {
this.x <<=
switch (side) {
case HEART ->
/* Comment before first case */
/* LHS comment */
// Inline comment
2;
Expand Down

0 comments on commit c438756

Please sign in to comment.