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(DefaultComesLast)[#289]: duplicate case fix #295

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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 @@ -75,7 +75,7 @@ public J.Switch visitSwitch(J.Switch switch_, P p) {
aCase.getStatements().isEmpty() && !foundNonEmptyCase) {
casesGroupedWithDefault.add(aCase);
} else {
if (defaultCase != null && defaultCase.getStatements().isEmpty() && !foundNonEmptyCase) {
if (defaultCase != null && defaultCase.getStatements().isEmpty() && foundNonEmptyCase) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (defaultCase != null && defaultCase.getStatements().isEmpty() && foundNonEmptyCase) {
if (nonNull(defaultCase) && isEmpty(defaultCase.getStatements()) && foundNonEmptyCase) {

https://stackoverflow.com/questions/28140193/purpose-of-objects-isnull-objects-nonnull
https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/CollectionUtils.html#isEmpty(java.util.Collection)

// the last case grouped with default can be non-empty. it will be flipped with
// the default case, including its statements
casesGroupedWithDefault.add(aCase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Test {
case 1:
break;
case 2:
break;
break;
default:
System.out.println("default");
break;
Expand Down Expand Up @@ -264,4 +264,39 @@ boolean foo() {
)
);
}

@Test
void defaultComesLastCreateDuplicate() {
rewriteRun(
//language=java
java(
"""
class Test {
int n;
boolean foo() {
switch (n) {
default:
case DERIVATION_MODE:
System.out.println("default");
break;
}
}
}
""",
"""
class Test {
int n;
boolean foo() {
switch (n) {
case DERIVATION_MODE:
default:
System.out.println("default");
break;
}
}
}
"""
)
);
}
}
Loading