Skip to content

Recipe FallThrough: A switch inside a switch incorrectly handled. Results in non compilable code. #173

Closed

Description

@jbessels

We have a few classes which contain a switch inside a switch. Even 3+ levels deep. Recipe FallThrough does not seem to be able to handle this correctly - all the time??. It breaks at least for the example beneath. Two break statements are added by it. The first one results in the IDE in ' unreachable statement'. When compiling I get two errors: First ' unreachable statement' and ' missing return statement' on the closing } char of the method.

Before
A simplified bogus example

public String funWithtWithSwitches() {
        int day = 2;
        switch (day) {
            case 1:
                return "Monday";
            case 2:
                final int month = 1;
                switch (month) {
                    case 1:
                        return "January";
                    case 2:
                        return "Febuary";
                    default:
                        return "no valid month";
                }
            default:
                return "No valid day";
        }
    }

After

    public String funWithtWithSwitches() {
        int day = 2;
        switch (day) {
            case 1:
                return "Monday";
            case 2:
            final int month = 1;
            switch (month) {
                case 1:
                    return "January";
                case 2:
                    return "Febuary";
                default:
                    return "no valid month";
                break; // break added. "Result Unreachable statement"
            }
            break; // break added
            default:
                return "No valid day";
        }

The screenshot contains 2 empty lines which I had removed above for clarity.
image

Activity

  1. added
    bug
    on Apr 3, 2023
  2. knutwannheden commented on Apr 3, 2023

    @knutwannheden
    Contributor

    The problem here is that the logic which checks if the block in a switch case completes normally or not doesn't consider all cases. A switch statement which in all its cases does a return is one such example. I think there are probably others which are currently not caught by the recipe. While for example while (true) return; and try { return; } finally {} should be considered such cases, if (true) return; should not. This type of checking is probably not that uncommon. We should introduce a utility which recipes can use for this purpose.

  3. moved this to Up Next in OpenRewriteon Sep 26, 2023
  4. timtebeek commented on Sep 26, 2023

    @timtebeek
    Member
  5. moved this from Up Next to Done in OpenRewriteon Oct 17, 2023
  6. jbessels commented on Oct 19, 2023

    @jbessels
    ContributorAuthor

    I've retested the recipe. It works!!
    Now no changes are made to the 7 files I had to use tags for.
    Thanks a lot

  7. added a commit that references this issue on Feb 21, 2024
    a66d927
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

    bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions