-
Notifications
You must be signed in to change notification settings - Fork 57
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
Unnecessary return
as last statement in void
method
#388
Unnecessary return
as last statement in void
method
#388
Conversation
src/main/java/org/openrewrite/staticanalysis/UnnecessaryReturnAsLastStatement.java
Outdated
Show resolved
Hide resolved
src/test/java/org/openrewrite/staticanalysis/UnnecessaryReturnAsLastStatementTest.java
Outdated
Show resolved
Hide resolved
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
src/main/java/org/openrewrite/staticanalysis/UnnecessaryReturnAsLastStatement.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/UnnecessaryReturnAsLastStatement.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/UnnecessaryReturnAsLastStatement.java
Outdated
Show resolved
Hide resolved
src/test/java/org/openrewrite/staticanalysis/UnnecessaryReturnAsLastStatementTest.java
Show resolved
Hide resolved
Co-authored-by: Tim te Beek <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think some more tests would be good. For example removing the return
here would be correct:
Consumer<Integer> c = i -> {
return;
};
while removing the return here would not be correct:
while (true) {
return;
}
Although it could also be argued that changing the first is incorrect, as it is a return from a lambda rather than a method and that isn't how the recipe is documented.
src/main/java/org/openrewrite/staticanalysis/UnnecessaryReturnAsLastStatement.java
Outdated
Show resolved
Hide resolved
Added some in 3325148. Kindly requesting another review. |
BTW, is there some easy way to run the Code Suggester locally to avoid the noise in the pull request itself? |
Yes sure! What I've done is take this file and put it in scratch, then run it through the IntelliJ Ultimate bundled support for OpenRewrite. |
Had another look; thanks so far @mccartney ; Since I think you'll enjoy the puzzle I've added a new unit test that fails. Let me know if you can't or won't enjoy working it out. :) |
|
55a093c
to
6ba0e18
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved from my side already; thanks for the thorough tests!
I've replaced the three methods you had with the same name but different arguments with a single recursive method, as the flow was harder to follow across those individual methods.
Saving others a click: unrelated test failure now in:
|
Indeed. I've been struggling for a while with that |
The type assignability check in Java is tricky. I think we might even have some cases where we can't do it correctly, as we don't have all the information we need in our type attribution. In any case I think this PR should now fix the test failure we have here: |
If I read the GHA output correctly, the failure is with:
now. |
That was resolved on main. I just merged in the main branch. |
@knutwannheden any last thoughts before we merge? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
Thanks both! As a quick check I'm running this recipe against all Apache projects: https://app.moderne.io/results/5TXPCrrF0 |
All the changes in that recipe run look good to me. |
I am hesitant to do this as the Moderne app requires kind of generous permissions (read/write code to all my private repos) to even see the changes. |
Rest assured we're not after your private projects at all; we merely need access to your email address to identify you, and then access to your projects if you choose to fork any OSS projects. I do see how the dialog could come across differently; we're exploring options on our side for that in parallel, but there should be no reason not to use the platform. |
What's changed?
Adding a new recipe to remove
return
if it's the last statement of avoid
method.What's your motivation?
return
is unnecessary as the last statement in avoid
method #75Checklist