Skip to content
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 @@ -70,26 +70,32 @@ public J visitIf(J.If iff, ExecutionContext ctx) {
Cursor parent = getCursor().getParentTreeCursor();

if (parent.getValue() instanceof J.Block &&
parent.getParentOrThrow().getValue() instanceof J.MethodDeclaration &&
thenHasOnlyReturnStatement(iff) &&
elseWithOnlyReturn(i)) {
parent.getParentOrThrow().getValue() instanceof J.MethodDeclaration &&
thenHasOnlyReturnStatement(iff) &&
elseWithOnlyReturn(i)) {
List<Statement> followingStatements = followingStatements();
Optional<Expression> singleFollowingStatement = Optional.ofNullable(followingStatements.isEmpty() ? null : followingStatements.get(0))
.flatMap(stat -> Optional.ofNullable(stat instanceof J.Return ? (J.Return) stat : null))
.filter(r -> r.getComments().isEmpty())
.map(J.Return::getExpression);

if (followingStatements.isEmpty() || singleFollowingStatement.map(r -> isLiteralFalse(r) || isLiteralTrue(r)).orElse(false)) {
J.Return return_ = getReturnIfOnlyStatementInThen(iff).orElse(null);
assert return_ != null;

// Do not remove comments that are attached to the return statement
if (!return_.getComments().isEmpty() || hasElseWithComment(i.getElsePart())) {
return i;
}

Expression ifCondition = i.getIfCondition().getTree();

if (isLiteralTrue(return_.getExpression())) {
if (singleFollowingStatement.map(this::isLiteralFalse).orElse(false) && i.getElsePart() == null) {
doAfterVisit(new DeleteStatement<>(followingStatements().get(0)));
return maybeAutoFormat(return_, return_.withExpression(ifCondition), ctx, parent);
} else if (!singleFollowingStatement.isPresent() &&
getReturnExprIfOnlyStatementInElseThen(i).map(this::isLiteralFalse).orElse(false)) {
getReturnExprIfOnlyStatementInElseThen(i).map(this::isLiteralFalse).orElse(false)) {
if (i.getElsePart() != null) {
doAfterVisit(new DeleteStatement<>(i.getElsePart().getBody()));
}
Expand Down Expand Up @@ -185,6 +191,23 @@ private Optional<Expression> getReturnExprIfOnlyStatementInElseThen(J.If iff2) {

return Optional.empty();
}

private boolean hasElseWithComment(J.If.Else else_) {
if (else_ == null || else_.getBody() == null) {
return false;
}
if (!else_.getComments().isEmpty()) {
return true;
}
if (!else_.getBody().getComments().isEmpty()) {
return true;
}
if (else_.getBody() instanceof J.Block
&& !((J.Block) else_.getBody()).getStatements().get(0).getComments().isEmpty()) {
return true;
}
return false;
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.openrewrite.staticanalysis;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.test.RecipeSpec;
Expand All @@ -34,6 +35,7 @@ public void defaults(RecipeSpec spec) {
@Test
void simplifyBooleanReturn() {
rewriteRun(
//language=java
java(
"""
public class A {
Expand Down Expand Up @@ -74,6 +76,7 @@ static boolean isOddMillis() {
@Test
void dontSimplifyToReturnUnlessLastStatement() {
rewriteRun(
//language=java
java(
"""
public class A {
Expand Down Expand Up @@ -105,6 +108,7 @@ public boolean absurdEquals(Object o) {
@Test
void nestedIfsWithNoBlock() {
rewriteRun(
//language=java
java(
"""
public class A {
Expand All @@ -123,6 +127,7 @@ public boolean absurdEquals(Object o) {
@Test
void dontAlterWhenElseIfPresent() {
rewriteRun(
//language=java
java(
"""
public class A {
Expand All @@ -146,6 +151,7 @@ else if (n == 2) {
@Test
void dontAlterWhenElseContainsSomethingOtherThanReturn() {
rewriteRun(
//language=java
java(
"""
public class A {
Expand All @@ -167,6 +173,7 @@ public boolean foo(int n) {
@Test
void onlySimplifyToReturnWhenLastStatement() {
rewriteRun(
//language=java
java(
"""
import java.util.*;
Expand All @@ -188,6 +195,7 @@ public static boolean deepEquals(List<byte[]> l, List<byte[]> r) {
@Test
void wrapNotReturnsOfTernaryIfConditionsInParentheses() {
rewriteRun(
//language=java
java(
"""
public class A {
Expand All @@ -211,4 +219,92 @@ public boolean equals(Object o) {
)
);
}

@Nested
class RetainComments {
@Test
void onIfReturn() {
rewriteRun(
//language=java
java(
"""
class A {
boolean foo(int n) {
if (n == 1) {
// A comment that provides important context
return true;
}
else {
return false;
}
}
}
"""
)
);
}

@Test
void onElseBlockReturn() {
rewriteRun(
//language=java
java(
"""
class A {
boolean foo(int n) {
if (n == 1) {
return true;
}
else {
// A comment that provides important context
return false;
}
}
}
"""
)
);
}

@Test
void onElseReturn() {
rewriteRun(
//language=java
java(
"""
class A {
boolean foo(int n) {
if (n == 1) {
return true;
}
else
// A comment that provides important context
return false;
}
}
"""
)
);
}

@Test
void onImpliedElse() {
rewriteRun(
//language=java
java(
"""
class A {
boolean foo(int n) {
if (n == 1) {
return true;
}
// A comment that provides important context
return false;
}
}
"""
)
);
}
}
}