From 921e27f1d5554db650d04ef37034811fe81502a6 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 22 Jul 2025 09:32:38 +0000 Subject: [PATCH] refactor: Unwrap else block after return or throw statement Use this link to re-run the recipe: https://app.moderne.io/recipes/org.openrewrite.staticanalysis.UnwrapElseAfterReturn?organizationId=ODQ2MGExMTUtNDg0My00N2EwLTgzMGMtNGE1NGExMTBmZDkw Co-authored-by: Moderne --- .../openrewrite/python/remote/Extensions.java | 10 ++++---- .../org/openrewrite/python/PythonVisitor.java | 3 ++- .../python/format/PythonSpacesVisitor.java | 24 +++++++++---------- .../python/internal/PythonPrinter.java | 10 ++++---- .../java/org/openrewrite/python/tree/Py.java | 14 ++++++----- .../org/openrewrite/python/tree/PySpace.java | 14 +++++------ 6 files changed, 36 insertions(+), 39 deletions(-) diff --git a/rewrite-python-remote/src/main/java/org/openrewrite/python/remote/Extensions.java b/rewrite-python-remote/src/main/java/org/openrewrite/python/remote/Extensions.java index 7718c29c..7dc59e39 100644 --- a/rewrite-python-remote/src/main/java/org/openrewrite/python/remote/Extensions.java +++ b/rewrite-python-remote/src/main/java/org/openrewrite/python/remote/Extensions.java @@ -152,7 +152,7 @@ protected ReceiverContext.DetailsReceiver computeValue(Class nod return (leftPadded, type, ctx) -> { if (leftPadded != null) { leftPadded = leftPadded.withBefore(ctx.receiveNonNullNode(leftPadded.getBefore(), Extensions::receiveSpace)); - leftPadded = leftPadded.withElement(ctx.receiveNonNullNode((Space) leftPadded.getElement(), Extensions::receiveSpace)); + leftPadded = leftPadded.withElement(ctx.receiveNonNullNode((Space)leftPadded.getElement(), Extensions::receiveSpace)); leftPadded = leftPadded.withMarkers(ctx.receiveNonNullNode(leftPadded.getMarkers(), ctx::receiveMarkers)); } else { leftPadded = new JLeftPadded<>( @@ -163,9 +163,8 @@ protected ReceiverContext.DetailsReceiver computeValue(Class nod } return leftPadded; }; - } else { - throw new IllegalArgumentException("Unsupported type: " + nodeType); } + throw new IllegalArgumentException("Unsupported type: " + nodeType); } }; @@ -218,7 +217,7 @@ protected ReceiverContext.DetailsReceiver computeValue(Class no if (nodeType == Space.class) { return (rightPadded, type, ctx) -> { if (rightPadded != null) { - rightPadded = rightPadded.withElement(ctx.receiveNonNullNode((Space) rightPadded.getElement(), Extensions::receiveSpace)); + rightPadded = rightPadded.withElement(ctx.receiveNonNullNode((Space)rightPadded.getElement(), Extensions::receiveSpace)); rightPadded = rightPadded.withAfter(ctx.receiveNonNullNode(rightPadded.getAfter(), Extensions::receiveSpace)); rightPadded = rightPadded.withMarkers(ctx.receiveNonNullNode(rightPadded.getMarkers(), ctx::receiveMarkers)); } else { @@ -230,9 +229,8 @@ protected ReceiverContext.DetailsReceiver computeValue(Class no } return rightPadded; }; - } else { - throw new IllegalArgumentException("Unsupported type: " + nodeType); } + throw new IllegalArgumentException("Unsupported type: " + nodeType); } }; diff --git a/rewrite-python/src/main/java/org/openrewrite/python/PythonVisitor.java b/rewrite-python/src/main/java/org/openrewrite/python/PythonVisitor.java index 2bf244e0..e215cbb3 100644 --- a/rewrite-python/src/main/java/org/openrewrite/python/PythonVisitor.java +++ b/rewrite-python/src/main/java/org/openrewrite/python/PythonVisitor.java @@ -557,7 +557,8 @@ public Space visitSpace(@Nullable Space space, PySpace.Location loc, P p) { //noinspection ConstantValue if (space == Space.EMPTY || space == Space.SINGLE_SPACE || space == null) { return space; - } else if (space.getComments().isEmpty()) { + } + if (space.getComments().isEmpty()) { return space; } return visitSpace(space, Space.Location.LANGUAGE_EXTENSION, p); diff --git a/rewrite-python/src/main/java/org/openrewrite/python/format/PythonSpacesVisitor.java b/rewrite-python/src/main/java/org/openrewrite/python/format/PythonSpacesVisitor.java index 2ff38924..2c94b5be 100644 --- a/rewrite-python/src/main/java/org/openrewrite/python/format/PythonSpacesVisitor.java +++ b/rewrite-python/src/main/java/org/openrewrite/python/format/PythonSpacesVisitor.java @@ -90,11 +90,11 @@ JContainer spaceBefore(JContainer container, boolean spaceBefore) { if (spaceBefore && notSingleSpace(container.getBefore().getWhitespace()) && doesNotContainNewLine(container.getBefore().getWhitespace())) { return container.withBefore(container.getBefore().withWhitespace(" ")); - } else if (!spaceBefore && onlySpacesAndNotEmpty(container.getBefore().getWhitespace()) && doesNotContainNewLine(container.getBefore().getWhitespace())) { + } + if (!spaceBefore && onlySpacesAndNotEmpty(container.getBefore().getWhitespace()) && doesNotContainNewLine(container.getBefore().getWhitespace())) { return container.withBefore(container.getBefore().withWhitespace("")); - } else { - return container; } + return container; } private static List spaceLastCommentSuffix(List comments, boolean spaceSuffix) { @@ -105,11 +105,11 @@ private static List spaceLastCommentSuffix(List comments, bool private static Comment spaceSuffix(Comment comment, boolean spaceSuffix) { if (spaceSuffix && notSingleSpace(comment.getSuffix()) && doesNotContainNewLine(comment.getSuffix())) { return comment.withSuffix(" "); - } else if (!spaceSuffix && onlySpacesAndNotEmpty(comment.getSuffix()) && doesNotContainNewLine(comment.getSuffix())) { + } + if (!spaceSuffix && onlySpacesAndNotEmpty(comment.getSuffix()) && doesNotContainNewLine(comment.getSuffix())) { return comment.withSuffix(""); - } else { - return comment; } + return comment; } T spaceBefore(T j, boolean spaceBefore) { @@ -119,11 +119,11 @@ T spaceBefore(T j, boolean spaceBefore) { if (spaceBefore && notSingleSpace(j.getPrefix().getWhitespace()) && doesNotContainNewLine(j.getPrefix().getWhitespace())) { return j.withPrefix(j.getPrefix().withWhitespace(" ")); - } else if (!spaceBefore && onlySpacesAndNotEmpty(j.getPrefix().getWhitespace()) && doesNotContainNewLine(j.getPrefix().getWhitespace())) { + } + if (!spaceBefore && onlySpacesAndNotEmpty(j.getPrefix().getWhitespace()) && doesNotContainNewLine(j.getPrefix().getWhitespace())) { return j.withPrefix(j.getPrefix().withWhitespace("")); - } else { - return j; } + return j; } JRightPadded spaceAfter(JRightPadded container, boolean spaceAfter) { @@ -135,11 +135,11 @@ JRightPadded spaceAfter(JRightPadded container, boolean spac if (spaceAfter && notSingleSpace(container.getAfter().getWhitespace()) && doesNotContainNewLine(container.getAfter().getWhitespace())) { return container.withAfter(container.getAfter().withWhitespace(" ")); - } else if (!spaceAfter && onlySpacesAndNotEmpty(container.getAfter().getWhitespace()) && doesNotContainNewLine(container.getAfter().getWhitespace())) { + } + if (!spaceAfter && onlySpacesAndNotEmpty(container.getAfter().getWhitespace()) && doesNotContainNewLine(container.getAfter().getWhitespace())) { return container.withAfter(container.getAfter().withWhitespace("")); - } else { - return container; } + return container; } /** diff --git a/rewrite-python/src/main/java/org/openrewrite/python/internal/PythonPrinter.java b/rewrite-python/src/main/java/org/openrewrite/python/internal/PythonPrinter.java index 4187ffb0..fb46b606 100755 --- a/rewrite-python/src/main/java/org/openrewrite/python/internal/PythonPrinter.java +++ b/rewrite-python/src/main/java/org/openrewrite/python/internal/PythonPrinter.java @@ -50,10 +50,9 @@ public J visit(@Nullable Tree tree, PrintOutputCapture

p) { if (!(tree instanceof Py)) { // re-route printing to the Java printer return delegate.visitNonNull(requireNonNull(tree), p); - } else { - //noinspection DataFlowIssue - return super.visit(tree, p); } + //noinspection DataFlowIssue + return super.visit(tree, p); } @Override @@ -704,10 +703,9 @@ public J visit(@Nullable Tree tree, PrintOutputCapture

p) { if (tree instanceof Py) { // re-route printing back up to Python printer return PythonPrinter.this.visitNonNull(tree, p); - } else { - //noinspection DataFlowIssue - return super.visit(tree, p); } + //noinspection DataFlowIssue + return super.visit(tree, p); } @Override diff --git a/rewrite-python/src/main/java/org/openrewrite/python/tree/Py.java b/rewrite-python/src/main/java/org/openrewrite/python/tree/Py.java index 82653661..b9b25e07 100644 --- a/rewrite-python/src/main/java/org/openrewrite/python/tree/Py.java +++ b/rewrite-python/src/main/java/org/openrewrite/python/tree/Py.java @@ -749,9 +749,10 @@ public

J acceptPython(PythonVisitor

v, P p) { @Override public @Nullable JavaType getType() { if (reference instanceof Expression) { - return ((Expression) reference).getType(); - } else if (reference instanceof TypedTree) { - return ((TypedTree) reference).getType(); + return ((Expression)reference).getType(); + } + if (reference instanceof TypedTree) { + return ((TypedTree)reference).getType(); } return null; } @@ -760,9 +761,10 @@ public

J acceptPython(PythonVisitor

v, P p) { @Override public ExpressionTypeTree withType(@Nullable JavaType type) { if (reference instanceof Expression) { - return withReference(((Expression) reference).withType(type)); - } else if (reference instanceof TypedTree) { - return withReference(((TypedTree) reference).withType(type)); + return withReference(((Expression)reference).withType(type)); + } + if (reference instanceof TypedTree) { + return withReference(((TypedTree)reference).withType(type)); } return this; } diff --git a/rewrite-python/src/main/java/org/openrewrite/python/tree/PySpace.java b/rewrite-python/src/main/java/org/openrewrite/python/tree/PySpace.java index 6eb026f9..c9e14d86 100644 --- a/rewrite-python/src/main/java/org/openrewrite/python/tree/PySpace.java +++ b/rewrite-python/src/main/java/org/openrewrite/python/tree/PySpace.java @@ -37,11 +37,10 @@ public static final class SpaceBuilder { private String finishWhitespace() { if (whitespaceBuilder == null) { return ""; - } else { - String ws = whitespaceBuilder.toString(); - whitespaceBuilder.setLength(0); - return ws; } + String ws = whitespaceBuilder.toString(); + whitespaceBuilder.setLength(0); + return ws; } private void finishComment() { @@ -101,11 +100,10 @@ public static Space appendWhitespace(Space space, String whitespace) { comment -> comment.withSuffix(comment.getSuffix() + whitespace) ) ); - } else { - return space.withWhitespace( - space.getWhitespace() + whitespace - ); } + return space.withWhitespace( + space.getWhitespace() + whitespace + ); } public static Space appendComment(Space space, String commentWithHash) {