Skip to content

feat(migrate): support return statements in IfElseIfConstructToSwitch recipe#1162

Open
arnabnandy7 wants to merge 1 commit into
openrewrite:mainfrom
arnabnandy7:fix/switch-return-support
Open

feat(migrate): support return statements in IfElseIfConstructToSwitch recipe#1162
arnabnandy7 wants to merge 1 commit into
openrewrite:mainfrom
arnabnandy7:fix/switch-return-support

Conversation

@arnabnandy7

@arnabnandy7 arnabnandy7 commented Jul 3, 2026

Copy link
Copy Markdown

What's changed?

Allows converting if-else-if chains containing return statements to switch statements.

  • Removed the blanket returns(...) guard in validatePotentialCandidate() within IfElseIfConstructToSwitch.java.
  • Updated buildSwitchTemplate() to check if the branch statement is a bare J.Return. If so, it wraps it in block braces at the template-compilation level (case X -> {#{any()};}).
  • Updated the existing return-related tests in IfElseIfConstructToSwitchTest.java to expect successful switch conversions, and added new tests to cover mixed branches, braceless return chains, and nested returns.

What's your motivation?

Anything in particular you'd like reviewers to focus on?

  • The template compilation logic using {#{any()};} to ensure the mock code parsed in JavaTemplate is valid while avoiding double semicolons during replacement.
  • The test cases covering nested returns (e.g. returns inside lambda loops) to ensure they are preserved correctly.

Anyone you would like to review specifically?

Have you considered any alternatives or workarounds?

An alternative would have been to manually synthesize J.Block AST nodes in the visitor before applying JavaTemplate. However, wrapping the raw return in explicit braces at the template-string level is much cleaner and less error-prone.

Any additional context

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

… recipe

Signed-off-by: Arnab Nandy <arnab_nandy7@yahoo.com>
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Jul 3, 2026
@timtebeek timtebeek self-requested a review July 4, 2026 11:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

Enhance IfElseIfConstructToSwitch to support "return" statements

1 participant