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

False positive in MissingSwitchDefault with pattern in switch label #11220

Closed
nrmancuso opened this issue Jan 19, 2022 · 1 comment · Fixed by #11221
Closed

False positive in MissingSwitchDefault with pattern in switch label #11220

nrmancuso opened this issue Jan 19, 2022 · 1 comment · Fixed by #11221
Assignees
Milestone

Comments

@nrmancuso
Copy link
Member

nrmancuso commented Jan 19, 2022

After #11100 (comment):

I have read check documentation: https://checkstyle.sourceforge.io/config_coding.html#MissingSwitchDefault
I have downloaded the latest checkstyle from: https://checkstyle.org/cmdline.html#Download_and_Run
I have executed the cli and showed it below, as cli describes the problem better than 1,000 words

➜  company javac --enable-preview --release 17 Test.java                    
Note: Test.java uses preview features of Java SE 17.
Note: Recompile with -Xlint:preview for details.

➜  cat config.xml                                                   
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
        "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
        "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
    <module name="TreeWalker">
        <module name="MissingSwitchDefault">
        </module>
    </module>
</module>

➜  cat Test.java                                                    
public class Test {
    static void test4(Object o) {
        switch (o) { // ok
            case null, String s -> System.out.println("String, including null");
            default -> System.out.println("something else");
        }

        switch (o) { // ok
            case null, String s: System.out.println("String, including null"); break;
            default: System.out.println("something else");
        }

        // The following switch statements are equivalent:

        switch(o) {
            case null: default: // ok
                System.out.println("The rest (including null)");
        }

        switch(o) { // false positive, should be ok
            case null, default ->
                System.out.println("The rest (including null)");
        }

        switch(o) { // false positive, should be ok
            case null, default:
                throw new UnsupportedOperationException("not supported!");
        }
    }
}

➜  java -jar checkstyle-9.3-SNAPSHOT-all.jar -c config.xml Test.java
Starting audit...
[ERROR] Test.java:20:9: switch without "default" clause. [MissingSwitchDefault]
[ERROR] Test.java:25:9: switch without "default" clause. [MissingSwitchDefault]
Audit done.
Checkstyle ends with 2 errors.

I would expect Checkstyle to find no violations in this code example.

@nrmancuso nrmancuso self-assigned this Jan 19, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jan 20, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jan 20, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jan 20, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jan 20, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jan 20, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jan 20, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jan 20, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jan 20, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jan 20, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jan 20, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jan 20, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jan 20, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jan 20, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jan 23, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jan 23, 2022
github-actions bot pushed a commit to nrmancuso/checkstyle that referenced this issue Jan 23, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jan 23, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jan 23, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jan 24, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jan 26, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jan 26, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jan 26, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jan 27, 2022
@rnveach
Copy link
Member

rnveach commented Jan 28, 2022

Fix was merged

@rnveach rnveach added the bug label Jan 28, 2022
@rnveach rnveach added this to the 9.3 milestone Jan 28, 2022
MUzairS15 pushed a commit to MUzairS15/checkstyle that referenced this issue Jan 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants