Skip to content

Unwrap else after return in preceding if #645

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

Merged
merged 7 commits into from
Jul 22, 2025
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 @@ -126,11 +126,14 @@ private J.VariableDeclarations maybeFixVariableDeclarations(J.VariableDeclaratio
private boolean requiresSerialVersionField(@Nullable JavaType type) {
if (type == null) {
return false;
} else if (type instanceof JavaType.Primitive) {
}
if (type instanceof JavaType.Primitive) {
return true;
} else if (type instanceof JavaType.Array) {
}
if (type instanceof JavaType.Array) {
return requiresSerialVersionField(((JavaType.Array) type).getElemType());
} else if (type instanceof JavaType.Parameterized) {
}
if (type instanceof JavaType.Parameterized) {
JavaType.Parameterized parameterized = (JavaType.Parameterized) type;
if (parameterized.isAssignableTo("java.util.Collection") || parameterized.isAssignableTo("java.util.Map")) {
//If the type is either a collection or a map, make sure the type parameters are serializable. We
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ public J visitUnary(J.Unary unary, ExecutionContext ctx) {
private boolean isControlExpression(Expression expression) {
Cursor parentCursor = getCursor().getParentTreeCursor();
if (parentCursor.getValue() instanceof J.ControlParentheses &&
parentCursor.getParentTreeCursor().getValue() instanceof J.If) {
parentCursor.getParentTreeCursor().getValue() instanceof J.If) {
return true;
} else if (parentCursor.getValue() instanceof J.Ternary) {
}
if (parentCursor.getValue() instanceof J.Ternary) {
return ((J.Ternary) parentCursor.getValue()).getCondition() == expression;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
private boolean isConvertibleBigDecimalConstant(J elem) {
if (elem instanceof J.Literal) {
return true;
} else if (elem instanceof J.FieldAccess && ((J.FieldAccess) elem).getTarget().getType() instanceof JavaType.FullyQualified) {
}
if (elem instanceof J.FieldAccess && ((J.FieldAccess) elem).getTarget().getType() instanceof JavaType.FullyQualified) {
J.FieldAccess fa = (J.FieldAccess) elem;
return fa.getTarget().getType() != null && TypeUtils.isOfClassType(fa.getTarget().getType(), "java.math.BigDecimal");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,12 @@ public static boolean flatAdditiveExpressions(Expression expression, List<Expres
}

return flatAdditiveExpressions(b.getLeft(), expressionList) &&
flatAdditiveExpressions(b.getRight(), expressionList);
} else if (expression instanceof J.Literal ||
expression instanceof J.Identifier ||
expression instanceof J.MethodInvocation ||
expression instanceof J.Parentheses) {
flatAdditiveExpressions(b.getRight(), expressionList);
}
if (expression instanceof J.Literal ||
expression instanceof J.Identifier ||
expression instanceof J.MethodInvocation ||
expression instanceof J.Parentheses) {
expressionList.add(expression.withPrefix(Space.EMPTY));
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1800,7 +1800,8 @@ private static List<NameTree> getCaughtExceptions(J.Try.Catch aCatch) {
TypeTree typeExpr = aCatch.getParameter().getTree().getTypeExpression();
if (typeExpr instanceof J.MultiCatch) {
return ((J.MultiCatch) typeExpr).getAlternatives();
} else if (typeExpr != null) { // Can be J.Identifier or J.FieldAccess
}
if (typeExpr != null) { // Can be J.Identifier or J.FieldAccess
return singletonList(typeExpr);
}
return emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ public J.Block visitBlock(J.Block block, ExecutionContext ctx) {
boolean shouldReformat(Statement s) {
if (s instanceof J.If) {
return shouldReformat((J.If) s);
} else if (s instanceof Loop) {
}
if (s instanceof Loop) {
Statement body = ((Loop) s).getBody();
return !(body instanceof J.Block);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,8 @@ private List<J.Case> maybeReorderFallthroughCases(List<J.Case> cases, P p) {
} else if (defaultCase != null) {
if (!aCase.getStatements().isEmpty() && i != cases.size() - 1) {
return cases;
} else {
postDefaultCases.add(aCase);
}
postDefaultCases.add(aCase);
} else {
preDefaultCases.add(aCase);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ private boolean isEmptyBlock(Statement blockNode) {
J.Block block = (J.Block) blockNode;
if (EmptyBlockStyle.BlockPolicy.STATEMENT == emptyBlockStyle.getBlockPolicy()) {
return block.getStatements().isEmpty();
} else if (EmptyBlockStyle.BlockPolicy.TEXT == emptyBlockStyle.getBlockPolicy()) {
}
if (EmptyBlockStyle.BlockPolicy.TEXT == emptyBlockStyle.getBlockPolicy()) {
return block.getStatements().isEmpty() && block.getEnd().getComments().isEmpty();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,21 @@ public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations
if (maybeBlockOrGType.getParent() == null || maybeBlockOrGType.getParent().getParent() == null) {
// Groovy type.
return v;
} else {
J maybeClassDecl = maybeBlockOrGType
.getParentTreeCursor() // maybe J.ClassDecl or J.NewClass
.getValue();
if (!(maybeClassDecl instanceof J.ClassDeclaration || maybeClassDecl instanceof J.NewClass)) {
return v;
}
}
J maybeClassDecl = maybeBlockOrGType
.getParentTreeCursor() // maybe J.ClassDecl or J.NewClass
.getValue();
if (!(maybeClassDecl instanceof J.ClassDeclaration || maybeClassDecl instanceof J.NewClass)) {
return v;
}

if (!(maybeClassDecl instanceof J.NewClass) &&
J.ClassDeclaration.Kind.Type.Class != ((J.ClassDeclaration) maybeClassDecl).getKind()) {
return v;
}
if (!(maybeClassDecl instanceof J.NewClass) &&
J.ClassDeclaration.Kind.Type.Class != ((J.ClassDeclaration) maybeClassDecl).getKind()) {
return v;
}

if (!(variableDeclsCursor.getValue() instanceof J.VariableDeclarations)) {
return v;
}
if (!(variableDeclsCursor.getValue() instanceof J.VariableDeclarations)) {
return v;
}
Iterator<Cursor> clz = getCursor().getPathAsCursors(c -> c.getValue() instanceof J.ClassDeclaration);
if (clz.hasNext() && service(AnnotationService.class).matches(clz.next(), LOMBOK_VALUE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,16 @@ private J.VariableDeclarations maybeAddTypeExpression(J.VariableDeclarations mul
private @Nullable TypeTree buildTypeTree(@Nullable JavaType type, Space space) {
if (type == null || type instanceof JavaType.Unknown) {
return null;
} else if (type instanceof JavaType.Primitive) {
}
if (type instanceof JavaType.Primitive) {
return new J.Primitive(
Tree.randomId(),
space,
Markers.EMPTY,
(JavaType.Primitive) type
);
} else if (type instanceof JavaType.FullyQualified) {
}
if (type instanceof JavaType.FullyQualified) {

JavaType.FullyQualified fq = (JavaType.FullyQualified) type;

Expand Down Expand Up @@ -153,11 +155,11 @@ private J.VariableDeclarations maybeAddTypeExpression(J.VariableDeclarations mul
new JavaType.Parameterized(null, fq, fq.getTypeParameters())
);

} else {
maybeAddImport(fq);
return identifier;
}
} else if (type instanceof JavaType.Array) {
maybeAddImport(fq);
return identifier;
}
if (type instanceof JavaType.Array) {
JavaType.Array arrayType = (JavaType.Array) type;
// Get the base element type
JavaType elemType = arrayType.getElemType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,15 @@ private static boolean breaks(Statement s) {
if (s instanceof J.Block) {
List<Statement> statements = ((J.Block) s).getStatements();
return !statements.isEmpty() && breaks(statements.get(statements.size() - 1));
} else if (s instanceof J.If) {
}
if (s instanceof J.If) {
J.If iff = (J.If) s;
return iff.getElsePart() != null && breaks(iff.getThenPart());
} else if (s instanceof J.Label) {
}
if (s instanceof J.Label) {
return breaks(((J.Label) s).getStatement());
} else if (s instanceof J.Try) {
}
if (s instanceof J.Try) {
J.Try try_ = (J.Try) s;
if (try_.getFinally() != null && breaks(try_.getFinally())) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public J.ForLoop visitForLoop(J.ForLoop forLoop, ExecutionContext ctx) {
J.Unary u = (J.Unary) update;
if (u.getOperator() == J.Unary.Type.PreIncrement) {
return ((J.Unary) update).withOperator(J.Unary.Type.PostIncrement);
} else if (u.getOperator() == J.Unary.Type.PreDecrement) {
}
if (u.getOperator() == J.Unary.Type.PreDecrement) {
return ((J.Unary) update).withOperator(J.Unary.Type.PostDecrement);
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/org/openrewrite/staticanalysis/InlineVariable.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,18 @@ public J.Block visitBlock(J.Block block, ExecutionContext ctx) {
bl = bl.withStatements(ListUtils.map(statements, (i, statement) -> {
if (i == statements.size() - 2) {
return null;
} else if (i == statements.size() - 1) {
}
if (i == statements.size() - 1) {
if (statement instanceof J.Return) {
J.Return return_ = (J.Return) statement;
return return_.withExpression(requireNonNull(identDefinition.getInitializer())
.withPrefix(requireNonNull(return_.getExpression()).getPrefix()))
.withPrefix(requireNonNull(return_.getExpression()).getPrefix()))
.withPrefix(varDec.getPrefix().withComments(ListUtils.concatAll(varDec.getComments(), return_.getComments())));
} else if (statement instanceof J.Throw) {
}
if (statement instanceof J.Throw) {
J.Throw thrown = (J.Throw) statement;
return thrown.withException(requireNonNull(identDefinition.getInitializer())
.withPrefix(requireNonNull(thrown.getException()).getPrefix()))
.withPrefix(requireNonNull(thrown.getException()).getPrefix()))
.withPrefix(varDec.getPrefix().withComments(ListUtils.concatAll(varDec.getComments(), thrown.getComments())));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,18 +409,16 @@ public J visitBinary(J.Binary binary, Integer p) {
if (binary.getRight() instanceof J.InstanceOf) {
newRight = replacements.processInstanceOf((J.InstanceOf) binary.getRight(), widenedCursor);
} else if (binary.getRight() instanceof J.Parentheses &&
((J.Parentheses<?>) binary.getRight()).getTree() instanceof J.InstanceOf) {
@SuppressWarnings("unchecked")
J.Parentheses<J.InstanceOf> originalRight = (J.Parentheses<J.InstanceOf>) binary.getRight();
((J.Parentheses<?>) binary.getRight()).getTree() instanceof J.InstanceOf) {
@SuppressWarnings("unchecked") J.Parentheses<J.InstanceOf> originalRight = (J.Parentheses<J.InstanceOf>) binary.getRight();
newRight = originalRight.withTree(replacements.processInstanceOf(originalRight.getTree(), widenedCursor));
} else {
newRight = (Expression) visitNonNull(binary.getRight(), p, widenedCursor);
}
return b.withRight(newRight);
} else {
// The left side didn't change, so the right side doesn't need to see any introduced variable names
return b.withRight((Expression) visitNonNull(binary.getRight(), p));
}
// The left side didn't change, so the right side doesn't need to see any introduced variable names
return b.withRight((Expression) visitNonNull(binary.getRight(), p));
}

@Override
Expand Down Expand Up @@ -495,7 +493,8 @@ public String variableName(@Nullable JavaType type) {
if (style == Style.EXACT) {
//noinspection DataFlowIssue
return name;
} else if (type instanceof JavaType.FullyQualified) {
}
if (type instanceof JavaType.FullyQualified) {
String className = ((JavaType.FullyQualified) type).getClassName();
className = className.substring(className.lastIndexOf('.') + 1);
String baseName = null;
Expand Down Expand Up @@ -547,10 +546,12 @@ public String variableName(@Nullable JavaType type) {
break;
}
return candidate;
} else if (type instanceof JavaType.Primitive) {
}
if (type instanceof JavaType.Primitive) {
String keyword = ((JavaType.Primitive) type).getKeyword();
return style == Style.SHORT ? keyword.substring(0, 1) : keyword;
} else if (type instanceof JavaType.Array) {
}
if (type instanceof JavaType.Array) {
JavaType elemType = ((JavaType.Array) type).getElemType();
while (elemType instanceof JavaType.Array) {
elemType = ((JavaType.Array) elemType).getElemType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,21 @@ static J.MemberReference newInstanceMethodReference(Expression containing, Strin
JavaType.Class classType = (JavaType.Class) type;
if (classType.getFullyQualifiedName().equals("java.lang.Class")) {
return classType;
} else if (classType.getFullyQualifiedName().equals("java.lang.Object")) {
}
if (classType.getFullyQualifiedName().equals("java.lang.Object")) {
for (JavaType.Method method : classType.getMethods()) {
if (method.getName().equals("getClass")) {
return getClassType(method.getReturnType());
}
}
return null;
} else {
return getClassType(classType.getSupertype());
}
} else if (type instanceof JavaType.Parameterized) {
return getClassType(classType.getSupertype());
}
if (type instanceof JavaType.Parameterized) {
return getClassType(((JavaType.Parameterized) type).getType());
} else if (type instanceof JavaType.GenericTypeVariable) {
}
if (type instanceof JavaType.GenericTypeVariable) {
return getClassType(((JavaType.GenericTypeVariable) type).getBounds().get(0));
} else if (type instanceof JavaType.Array) {
return getClassType(((JavaType.Array) type).getElemType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ public J.Lambda visitLambda(J.Lambda lambda, ExecutionContext ctx) {
Space prefix = statement.getPrefix();
if (statement instanceof J.Return) {
Expression expression = ((J.Return) statement).getExpression();
if (prefix.getComments().isEmpty()) {
//noinspection DataFlowIssue
return l.withBody(expression);
} else if (expression != null) {
return l.withBody(expression.withPrefix(prefix));
}
if (prefix.getComments().isEmpty()) {
//noinspection DataFlowIssue
return l.withBody(expression);
}
if (expression != null) {
return l.withBody(expression.withPrefix(prefix));
}
} else if (statement instanceof J.MethodInvocation) {
if (prefix.getComments().isEmpty()) {
return l.withBody(statement);
} else {
return l.withBody(statement.withPrefix(prefix));
}
return l.withBody(statement.withPrefix(prefix));
}
}
}
Expand Down
Loading