Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug when deleting the first member from an enum. #4574

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading