Skip to content
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

Clean up "Convert if/else if/else chain with 3 blocks min to switch" in 2024-09 changes code semantic by not considering null #1714

Closed
FlorianKroiss opened this issue Oct 14, 2024 · 1 comment · Fixed by #1726
Assignees
Labels
bug Something isn't working
Milestone

Comments

@FlorianKroiss
Copy link

FlorianKroiss commented Oct 14, 2024

Consider the following example:

public class Dummy {

    enum TestEnum {
        A, B, C
    }

    public int doSomething(final TestEnum val) {
        if (val == TestEnum.A) {
            return 1;
        }
        else if (val == TestEnum.B) {
            return 2;
        }
        else if (val == TestEnum.C) {
            return 3;
        }
        return 4;
    }

}

Calling this method with null returns 4.

Applying the code clean up "Convert if/else if/else chain with 3 blocks min to switch" produces the following code

public class Dummy {

    enum TestEnum {
        A, B, C
    }

    public int doSomething(final TestEnum val) {
        switch ( val ) {
            case TestEnum.A:
                return 1;
            case TestEnum.B:
                return 2;
            case TestEnum.C:
                return 3;
            default:
                break;
        }
        return 4;
    }

}

The cleaned up code now throws a NullPointerException, when being called with null, instead of falling through to return 4;.
I guess in case null was not explicitly treated in the original code, then the correct clean up would be to use case null, default: instead of default:.

@jjohnstn jjohnstn self-assigned this Oct 15, 2024
@jjohnstn jjohnstn added bug Something isn't working and removed bug Something isn't working labels Oct 15, 2024
jjohnstn added a commit to jjohnstn/eclipse.jdt.ui-1 that referenced this issue Oct 21, 2024
- for < JVM 21 add an if/else that checks for null
- for JVM 21 and up, add case null
- modified SwitchFixCore and modified tests in CleanUpTest12
  to also not qualify enums if the switch value is an enum
- fixes eclipse-jdt#1714
@jjohnstn jjohnstn added this to the 4.34 M3 milestone Oct 21, 2024
jjohnstn added a commit that referenced this issue Oct 21, 2024
- for < JVM 21 add an if/else that checks for null
- for JVM 21 and up, add case null
- modified SwitchFixCore and modified tests in CleanUpTest12
  to also not qualify enums if the switch value is an enum
- fixes #1714
@jjohnstn
Copy link
Contributor

BTW: thanks @FlorianKroiss for reporting this. The cleanup has been changed to add the case null when the Java version allows it and to create an if statement otherwise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants