Skip to content

Commit 9cf8dc1

Browse files
committed
Don't crash on empty enum declarations
Fixes #281 MOE_MIGRATED_REVID=197211835
1 parent c68bb9c commit 9cf8dc1

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -834,10 +834,24 @@ public boolean visitEnumDeclaration(ClassTree node) {
834834
members.add(member);
835835
}
836836
if (enumConstants.isEmpty() && members.isEmpty()) {
837-
builder.open(ZERO);
838-
builder.blankLineWanted(BlankLineWanted.NO);
839-
token("}");
840-
builder.close();
837+
if (builder.peekToken().equals(Optional.of(";"))) {
838+
builder.open(plusTwo);
839+
builder.forcedBreak();
840+
token(";");
841+
builder.forcedBreak();
842+
dropEmptyDeclarations();
843+
builder.close();
844+
builder.open(ZERO);
845+
builder.forcedBreak();
846+
builder.blankLineWanted(BlankLineWanted.NO);
847+
token("}", plusTwo);
848+
builder.close();
849+
} else {
850+
builder.open(ZERO);
851+
builder.blankLineWanted(BlankLineWanted.NO);
852+
token("}");
853+
builder.close();
854+
}
841855
} else {
842856
builder.open(plusTwo);
843857
builder.blankLineWanted(BlankLineWanted.NO);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public enum Empty {}
2+
public enum Empty {;}
3+
public enum Empty {;;;}
4+
public enum Empty {; // comment
5+
;;}
6+
public enum Empty {;
7+
;; // comment
8+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public enum Empty {}
2+
3+
public enum Empty {
4+
;
5+
}
6+
7+
public enum Empty {
8+
;
9+
;;
10+
}
11+
12+
public enum Empty {
13+
; // comment
14+
;;
15+
}
16+
17+
public enum Empty {
18+
;
19+
;; // comment
20+
}

0 commit comments

Comments
 (0)