-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
False positive in MissingSwitchDefault for switch statement where null label is present #14068
Comments
I am on it |
@romani I have a doubt regarding this issue, The check is MissingSwitchDefault which seems to check whether the default keyword is present or not in the switch statement that's why a violation is raised. Can you please highlight this point This check does not validate switch statements that use pattern or null labels. Rationale: Switch statements that use pattern or null labels are checked by the compiler for exhaustiveness. This means that all possible inputs must be covered. |
@aayushRedHat You seemed to have copied the wording from our documentation. https://checkstyle.org/checks/coding/missingswitchdefault.html#Description
If this statement applies to the code example, then we are not expecting any violations. User has reported there is a violation. |
…itch statement where null label is present
…itch statement where null label is present
…itch statement where null label is present
…itch statement where null label is present
…itch statement where null label is present
…itch statement where null label is present
…itch statement where null label is present
…itch statement where null label is present
…itch statement where null label is present
…itch statement where null label is present
…itch statement where null label is present
…itch statement where null label is present
…itch statement where null label is present
…itch statement where null label is present
…itch statement where null label is present
…itch statement where null label is present
Closed via #14070 |
@nrmancuso , this is defect/bug. |
v10.12.7 is raising a False positive in MissingSwitchDefault when using an exhaustive enum switch statement. Very similar to this issue, should I open a new issue?
|
@Auto81, please create new issue on this |
@Auto81 This is not an "exhaustive switch" as enforced by the compiler. While this is conceptually "exhaustive" as written, this switch is semantically equivalent to: switch (i) {
case FIRST: System.out.println("FIRST"); break;
case SECOND: System.out.println("SECOND"); break;
} Consider:
Also consider:
Both examples above are compilable, and it is advisable in your case above to have a
Edit: TLDR; the arrow syntax in switch statements on non-pattern case label elements is not exhaustive. |
@nrmancuso Sorry but I don't understand what constitute an exhaustive switch then, in what case does checkstyle accept omitting the default case? |
@va1erian thanks for your question. You can omit the default case when the switch statement or expression is exhaustive by Java's definition. This includes all switch expressions, and any switch statements that have Pattern or null labels. This is an example of an exhaustive switch:
In the case above, we would not want Checkstyle to tell us we need a default label, because exhaustiveness is enforced by the compiler. Indeed it does not:
A good litmus test on our side going forward for this check as the switch grammar continues to evolve will be guided by the compiler; if a given switch meets the criteria to be checked by the compiler for exhaustiveness (i.e. will throw an error like the example above), then we have no need to require a default label. |
@nrmancuso thank you for the "exhaustive" explanation ;) much appreciated |
Check documentation https://checkstyle.sourceforge.io/checks/coding/missingswitchdefault.html#Description
# Run with java 21 /var/tmp $ javac MissingSwitchDefaultTest.java
/var/tmp $ cat config.xml
/var/tmp $ cat MissingSwitchDefaultTest.java
/var/tmp $ java -jar checkstyle-10.12.5-all.jar -c config.xml MissingSwitchDefaultTest.java Starting audit... [ERROR] /Users/kxbh697/temp/checkstyle/MissingSwitchDefaultTest.java:3:9: switch without "default" clause. [MissingSwitchDefault] Audit done. Checkstyle ends with 1 errors.
I'd expect this to have no violation. The documentation states that
This check does not validate switch statements that use pattern or null labels
, but here missing default is reported even though null case is present. The compiler correctly throws an error if case for FIRST or SECOND is removed, so exhaustiveness is being checked.The text was updated successfully, but these errors were encountered: