Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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,87 @@
/*
* 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.java.JavaIsoVisitor;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.Statement;

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;
}

List<Statement> statements = b.getStatements();
if (statements.isEmpty()) {
return b;
}
Statement lastStatement = statements.get(statements.size() - 1);
List<Statement> allButLast = statements.subList(0, statements.size() - 1);
if (lastStatement instanceof J.Return) {
return b.withStatements(allButLast);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing I am missing here is the check if the return has an expression or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. There's no check for void return type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In bcc951a adding two lines of defense:

  • the whole method being a void method
  • the return expression having an empty expression

Not fully sure if you prefer to have both (I've checked both work alone).

} else if (lastStatement instanceof J.If) {
J.If ifStatement = (J.If) lastStatement;
J.If.Else elze = ifStatement.getElsePart();
J.If newIf = ifStatement
.withThenPart(maybeRemoveReturnAsLastStatement(ifStatement.getThenPart()))
.withElsePart(elze.withBody(maybeRemoveReturnAsLastStatement(elze.getBody())));
List<Statement> newStatements = new ArrayList<>(allButLast);
newStatements.add(newIf);
return b.withStatements(newStatements);
} else {
return b;
}
}

@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
J.MethodDeclaration m = super.visitMethodDeclaration(method, ctx);
if (m == null) {
return m;
}
return m.withBody(maybeRemoveReturnAsLastStatement(m.getBody()));
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* 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");
}
}
"""));
}
}