Skip to content

Commit

Permalink
DuplicateBranches: don't crash on empty blocks
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 692998546
  • Loading branch information
cushon authored and Error Prone Team committed Nov 4, 2024
1 parent d173d85 commit b02e05b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@ private Description match(Tree tree, Tree thenTree, Tree elseTree, VisitorState
if (elseTree instanceof BlockTree) {
needsBraces = !state.getPath().getParentPath().getLeaf().getKind().equals(Kind.BLOCK);
var statements = ((BlockTree) elseTree).getStatements();
start = getStartPosition(statements.get(0));
end = state.getEndPosition(getLast(statements));
if (statements.isEmpty()) {
start = end;
} else {
start = getStartPosition(statements.get(0));
end = state.getEndPosition(getLast(statements));
}
}
String comments =
ErrorProneTokens.getTokens(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,29 @@ boolean g(boolean a, boolean b) {
""")
.doTest(TestMode.TEXT_MATCH);
}

@Test
public void negativeEmpty() {
BugCheckerRefactoringTestHelper.newInstance(DuplicateBranches.class, getClass())
.addInputLines(
"Test.java",
"""
class Test {
void f(boolean a) {
if (a) {
} else {
}
}
}
""")
.addOutputLines(
"Test.java",
"""
class Test {
void f(boolean a) {
}
}
""")
.doTest();
}
}

0 comments on commit b02e05b

Please sign in to comment.