-
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
Merged
timtebeek
merged 21 commits into
openrewrite:main
from
mccartney:return-as-last-statement
Nov 25, 2024
+401
−0
Merged
Changes from 8 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
aa40221
adding UnnecessaryReturnAsLastStatement
mccartney e8af765
licenseFormat
mccartney 148e4f4
remove public in test
mccartney db44470
ctx instead of executionContext
mccartney 7112d6e
Removing null check
mccartney bcc951a
Adding a check for Void return type and no return expression
mccartney 4a91e07
Remove unnecessary else branch
mccartney 411156e
Refactor, using ListUtils.mapLast()
mccartney 3325148
More test cases
mccartney d090cae
Optimize imports
mccartney b98a190
Merge branch 'main' into return-as-last-statement
mccartney cd9c717
Slight polish to read from top down; add failing test
timtebeek b48dd6a
Handling return statements in Else too
mccartney bbabc62
More test cases
mccartney cc8d393
Uniquely name each of the three methods
timtebeek 6ba0e18
Use a single recursive function
timtebeek 051fc45
Prevent changes without any effect
timtebeek 2381c7f
Merge branch 'main' into return-as-last-statement
timtebeek 8904648
Merge branch 'main' into return-as-last-statement
timtebeek fd57a2a
Merge branch 'main' into return-as-last-statement
knutwannheden b5f7d41
Add `UnnecessaryReturnAsLastStatement` to `CommonStaticAnalysis`
timtebeek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
src/main/java/org/openrewrite/staticanalysis/UnnecessaryReturnAsLastStatement.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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())); | ||
mccartney marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| return m; | ||
| } | ||
| }; | ||
| } | ||
| } | ||
123 changes: 123 additions & 0 deletions
123
src/test/java/org/openrewrite/staticanalysis/UnnecessaryReturnAsLastStatementTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
| """)); | ||
| } | ||
|
|
||
mccartney marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.