Skip to content

Commit

Permalink
Fix a bug when deleting the first member from an enum.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 674306475
  • Loading branch information
graememorgan authored and Error Prone Team committed Sep 13, 2024
1 parent 10c306a commit af9fdd2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1681,6 +1681,15 @@ public static SuggestedFix replaceIncludingComments(
if (previousMember == null) {
tokens = getTokensAfterOpeningBrace(tokens);
}
// There can be a lingering semi as the last token of the previous member in enums, e.g.:
// enum {
// A, B, C; <-- extra semi
// int deletingThisVariable;
// }
// Treat this as morally part of the previous member.
if (!tokens.isEmpty() && tokens.get(0).kind() == TokenKind.SEMI) {
tokens = tokens.subList(1, tokens.size());
}
if (tokens.isEmpty()) {
return SuggestedFix.replace(tree, replacement);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,28 @@ public void unusedField() {
.doTest();
}

@Test
public void unusedFieldRefactoringInEnum() {
refactoringHelper
.addInputLines(
"Test.java",
"enum UnusedField {",
" A, B, C;",
" private int notUsedInt;",
" UnusedField() {",
" notUsedInt = 1;",
" }",
"}")
.addOutputLines(
"Test.java", //
"enum UnusedField {",
" A, B, C;",
" UnusedField() {",
" }",
"}")
.doTest();
}

@Test
public void unusedLocalVarInitialized() {
helper
Expand Down

0 comments on commit af9fdd2

Please sign in to comment.