-
Notifications
You must be signed in to change notification settings - Fork 93
Unnecessary return as last statement in void method
#388
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
Changes from 4 commits
aa40221
e8af765
148e4f4
db44470
7112d6e
bcc951a
4a91e07
411156e
3325148
d090cae
b98a190
cd9c717
b48dd6a
bbabc62
cc8d393
6ba0e18
051fc45
2381c7f
8904648
fd57a2a
b5f7d41
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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); | ||
|
||
| } 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) { | ||
mccartney marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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"); | ||
| } | ||
| } | ||
| """)); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.