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

Java 14 SwitchExpression Check Support validation: WhitespaceAroundCheck #8687

Closed
nrmancuso opened this issue Aug 12, 2020 · 0 comments
Closed

Comments

@nrmancuso
Copy link
Member

Child of #8658
Check documentation: https://checkstyle.sourceforge.io/config_whitespace.html#WhitespaceAround

Checks that a token is surrounded by whitespace.

➜  src /usr/lib/jvm/java-14-openjdk/bin/javac --enable-preview --source 14 TestClass.java                               
➜  src 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="WhitespaceAround"/>
    </module>
</module>
➜  src cat TestClass.java 
public class TestClass {

    int howMany1(int k) {
        int x = 2;
        int number = 42;
        boolean bool = false;
        switch (k) {
            case 1:
                int y= (2); // violation
                break;
            case 2:{ // violation
                int otherNumber = 42;
                int z = x + 2;
                x = z;
                break;
            }
            case (3 ):{ // violation
                if(bool = true) { // violation
                }
                int a = x + 3;
                x = a;
                break;
            }
            default:
                int b = 4;
                x = b;
        }
        return x;
    }

    int howMany2(int k) {
        int x = 2;
        int number = 42;
        boolean bool = false;
        return switch (k) {
            case 1 ->{ // violation x2
                int y= (2); // violation
                yield y;
            }
            case ( 2) ->{ // violation x2
                int otherNumber = 42;
                int z = ( x + 2); 
                yield ( otherNumber); 
            }
            case (3 ) ->{ // violation x2
                if(bool = true ) { // violation
                }
                int a = x + 3;
                x = a;
                yield a;
            }
            default -> {
                int b = (2 ); 
                yield b;
            }
        };
    }

    int howMany3(int x) {
        int inner = 8;
        boolean bool = false;
        return switch (x) {
            case 1:
                int y= (2); // violation
                yield y;
            case 2:{ // violation
                int otherNumber = 42;
                int z = inner = x + 2;
                x = z;
                yield (otherNumber);
            }
            case (3 ): //violation
                if(bool = true ) { // violation
                }
                int a = x + 3;
                x = a;
                yield a;
            default:
                int b = (2 ); 
                yield b;
        };
    }

}
➜  src java -jar /home/nick/IdeaProjects/checkstyle/target/checkstyle-8.36-SNAPSHOT-all.jar -c config.xml TestClass.java
Starting audit...
[ERROR] /home/nick/Desktop/enhanced-switch/src/TestClass.java:9:22: '=' is not preceded with whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/enhanced-switch/src/TestClass.java:11:20: '{' is not preceded with whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/enhanced-switch/src/TestClass.java:17:23: '{' is not preceded with whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/enhanced-switch/src/TestClass.java:18:17: 'if' is not followed by whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/enhanced-switch/src/TestClass.java:36:20: '->' is not followed by whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/enhanced-switch/src/TestClass.java:36:22: '{' is not preceded with whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/enhanced-switch/src/TestClass.java:37:22: '=' is not preceded with whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/enhanced-switch/src/TestClass.java:40:23: '->' is not followed by whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/enhanced-switch/src/TestClass.java:40:25: '{' is not preceded with whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/enhanced-switch/src/TestClass.java:45:23: '->' is not followed by whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/enhanced-switch/src/TestClass.java:45:25: '{' is not preceded with whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/enhanced-switch/src/TestClass.java:46:17: 'if' is not followed by whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/enhanced-switch/src/TestClass.java:64:22: '=' is not preceded with whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/enhanced-switch/src/TestClass.java:66:20: '{' is not preceded with whitespace. [WhitespaceAround]
[ERROR] /home/nick/Desktop/enhanced-switch/src/TestClass.java:73:17: 'if' is not followed by whitespace. [WhitespaceAround]
Audit done.
Checkstyle ends with 15 errors.


This check works as intended, so I think that we can close this issue. The switch rule with a LAMBDA should count as two, since unlike the COLON in a regular switch, it should have whitespace around it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants