Skip to content

Commit

Permalink
fix empty do-while loop body
Browse files Browse the repository at this point in the history
  • Loading branch information
kunli2 committed Oct 31, 2023
1 parent 9b5e80c commit 9eb1640
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -435,14 +435,17 @@ public J visitDoubleColonExpression(KtDoubleColonExpression expression, Executio

@Override
public J visitDoWhileExpression(KtDoWhileExpression expression, ExecutionContext data) {
JRightPadded<Statement> body = null;
if (expression.getBody() != null) {
body = JRightPadded.build(requireNonNull(expression.getBody()).accept(this, data)
.withPrefix(prefix(expression.getBody().getParent())));
}

return new J.DoWhileLoop(
randomId(),
prefix(expression),
Markers.EMPTY,
// FIXME NPE if no body
JRightPadded.build(requireNonNull(expression.getBody()).accept(this, data)
.withPrefix(prefix(expression.getBody().getParent()))
),
body,
padLeft(prefix(expression.getWhileKeyword()), mapControlParentheses(requireNonNull(expression.getCondition()), data).withPrefix(prefix(expression.getLeftParenthesis())))
);
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/org/openrewrite/kotlin/tree/DoWhileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,17 @@ fun test() {
)
);
}

@Test
void emptyBody() {
rewriteRun(
kotlin(
"""
fun infinite() {
do while (true)
}
"""
)
);
}
}

0 comments on commit 9eb1640

Please sign in to comment.