Skip to content

Enhance IfElseIfConstructToSwitch to support "return" statements #776

Description

@Jenson3210

Currently the IfElseIfConstructToSwitch recipe does not convert to a switch if any of the parts would return.
However, if all branches would return, we could convert it to and it could be a potential candidate. Only the intermediate phase of having returns in the case is not valid, hence would not be picked up by:

    static String formatter(Object obj) {
        if (obj instanceof String s) {
            return s;
        } else if (obj instanceof Long l) {
            return String.valueOf(l);
        } else {
            throw new IllegalStateException("Unexpected value: " + obj);
        }
    }

could become

    static String formatter(Object obj) {
        return switch (obj) {
            case String s -> s;
            case Long l -> String.valueOf(l);
            default -> throw new IllegalStateException("Unexpected value: " + obj);
        };
    }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions