Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
@@ -0,0 +1,83 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.staticanalysis;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.Statement;
import org.openrewrite.java.tree.TypeUtils;

import java.util.ArrayList;
import java.util.List;

public class UnnecessaryReturnAsLastStatement extends Recipe {
@Override
public String getDisplayName() {
return "Unnecessary `return` as last statement in void method";
}

@Override
public String getDescription() {
return "Removes `return` from a `void` method if it's the last statement.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {
private Statement maybeRemoveReturnAsLastStatement(Statement s) {
if (s instanceof J.Block) {
return maybeRemoveReturnAsLastStatement((J.Block) s);
} else {
return s;
}
}

private J.Block maybeRemoveReturnAsLastStatement(J.Block b) {
if (b == null) {
return null;
}

return b.withStatements(ListUtils.mapLast(b.getStatements(), lastStatement -> {
if (lastStatement instanceof J.Return && ((J.Return) lastStatement).getExpression() == null) {
return null;
} else if (lastStatement instanceof J.If) {
J.If ifStatement = (J.If) lastStatement;
J.If.Else elze = ifStatement.getElsePart();
return ifStatement
.withThenPart(maybeRemoveReturnAsLastStatement(ifStatement.getThenPart()))
.withElsePart(elze.withBody(maybeRemoveReturnAsLastStatement(elze.getBody())));
} else {
return lastStatement;
}
}));
}

@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
J.MethodDeclaration m = super.visitMethodDeclaration(method, ctx);
if (TypeUtils.asPrimitive(m.getType()) == JavaType.Primitive.Void) {
return m.withBody(maybeRemoveReturnAsLastStatement(m.getBody()));
}
return m;
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.staticanalysis;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;

class UnnecessaryReturnAsLastStatementTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new UnnecessaryReturnAsLastStatement());
}

@Test
@DocumentExample
void simpleReturn() {
//language=java
rewriteRun(
java(
"""
class Hello {
void world() {
System.out.println("Hello world");
return;
}
}
""",
"""
class Hello {
void world() {
System.out.println("Hello world");
}
}
"""
)
);
}

@Test
void ifBranches() {
//language=java
rewriteRun(
java(
"""
class Hello {
void world(int i) {
if (i > 0) {
System.out.println("Positive");
return;
} else {
System.out.println("Zero or negative");
return;
}
}
}
""",
"""
class Hello {
void world(int i) {
if (i > 0) {
System.out.println("Positive");
} else {
System.out.println("Zero or negative");
}
}
}
"""
)
);
}

@Test
void ifIsNotTheLast() {
//language=java
rewriteRun(
java(
"""
class Hello {
void world(int i) {
if (i > 0) {
System.out.println("Positive");
return;
} else {
System.out.println("Zero or negative");
}
System.out.println("Some extra logic");
}
}
"""));
}

@Test
void notChangingNonVoidMethods() {
//language=java
rewriteRun(
java(
"""
class Hello {
int world(int i) {
return i + 436;
}
}
"""));
}

}