Skip to content

Unwrap else after return in preceding if #645

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
merged 7 commits into from
Jul 22, 2025
Merged

Conversation

timtebeek
Copy link
Member

@timtebeek timtebeek commented Jul 21, 2025

Added a new recipe, polished it up, and ran it on this repository to flush out any potential issues. Seemed straightforward enough and nicely reduces needless indentation. Let me know if you agree and then we can run this more broadly, and enforce it going forward.

@timtebeek timtebeek self-assigned this Jul 21, 2025
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Jul 21, 2025
@timtebeek timtebeek moved this from In Progress to Ready to Review in OpenRewrite Jul 21, 2025
@timtebeek timtebeek marked this pull request as ready for review July 21, 2025 16:21
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the actual recipe that needs review.

Copy link
Contributor

@greg-at-moderne greg-at-moderne left a comment

Choose a reason for hiding this comment

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

Another test case I'd add is with nested ifs in the primary branch. Something like:

if (someCondition) {
  // default logic
  if (somethingRare) {
     return "terminate all processing";
  }
} else {
  // non-standard logic
}

IMHO no changes should be made here.

Comment on lines +343 to +351
if ("one".equals(str)) {
return 1;
} else if ("two".equals(str)) {
return 2;
} else if ("three".equals(str)) {
return 3;
} else {
return Integer.MAX_VALUE;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be interesting to add a test case where only some of the ifs have the return here. Like only the two branch. That's similar to ifWithoutReturnNotChanged test case, but with chaining.

@greg-at-moderne
Copy link
Contributor

Also, not sure if it should go into the same recipe, or a separate one. The same kind of analysis and changes could be made with throw statements.

if (creditCardNumber.length() < 16) {
   throw new IllegalArgumentException("No shopping for you");
} else {
   // here comes the real logic which could be unwrapped
}

@timtebeek timtebeek merged commit 9092e3c into main Jul 22, 2025
2 checks passed
@timtebeek timtebeek deleted the unwrap-else-after-return branch July 22, 2025 09:09
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Jul 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants