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

Java14 Full Records Support Check Validation: BooleanExpressionComplexityCheck #8501

Closed
nrmancuso opened this issue Jul 16, 2020 · 1 comment

Comments

@nrmancuso
Copy link
Member

nrmancuso commented Jul 16, 2020

Child of #8452
Check documentation: https://checkstyle.sourceforge.io/config_metrics.html#BooleanExpressionComplexity

From check documentation:
Restricts the number of boolean operators (&&, ||, &, | and ^) in an expression.

➜  full-record-grammar /usr/lib/jvm/java-14-openjdk/bin/javac --enable-preview --source 14 TestClass.java
Note: TestClass.java uses preview language features.
Note: Recompile with -Xlint:preview for details.
➜  full-record-grammar 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="BooleanExpressionComplexity">
          <property name="max" value="3"/>
      </module>
  </module>
</module>
➜  full-record-grammar cat TestClass.java
// as fields
record MyRecord1(boolean a, boolean b) {

    private boolean myBool() {
       return  (a & b) ^ (a || b) | a; // violation
    }

}

record MyRecord2(String myString, boolean a, boolean b) {

    // in compact ctor
    public MyRecord2{
        boolean d = (a & b) ^ (a || b) | a; // should be violation
    }
}

record MyRecord3(int x) {

    // in ctor
    MyRecord3(){
        this(3);
        boolean b = true;
        boolean a = true;
        boolean d = (a & b) ^ (a || b) | a; // violation
    }
}

record MyRecord4(int y) {
    private record MyRecord5(int z) {
        // in nested record in compact ctor
        public MyRecord5{
            boolean b = false;
            boolean a = true;
            boolean d = (a & b) ^ (a || b) | a; // should be violation
        }
    }

}

➜  full-record-grammar java $RUN_LOCALE -jar ~/IdeaProjects/checkstyle/target/checkstyle-8.35-SNAPSHOT-all.jar -c config.xml TestClass.java
Starting audit...
[ERROR] /home/nick/Desktop/full-record-grammar/TestClass.java:5:8: Boolean expression complexity is 4 (max allowed is 3). [BooleanExpressionComplexity]
[ERROR] /home/nick/Desktop/full-record-grammar/TestClass.java:25:19: Boolean expression complexity is 4 (max allowed is 3). [BooleanExpressionComplexity]
Audit done.
Checkstyle ends with 2 errors.

We just need to add support for compact constructors to this check, since it works well in the body of a record definition.

@pbludov pbludov added this to To do in Java 14 syntax features via automation Jul 18, 2020
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Aug 8, 2020
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Aug 8, 2020
@pbludov pbludov added this to the 8.36 milestone Aug 8, 2020
@pbludov
Copy link
Member

pbludov commented Aug 8, 2020

fix is merged.

@pbludov pbludov closed this as completed Aug 8, 2020
Java 14 syntax features automation moved this from To do to Done Aug 8, 2020
@pbludov pbludov added the bug label Aug 8, 2020
shiliyu pushed a commit to shiliyu/checkstyle that referenced this issue Sep 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Development

No branches or pull requests

2 participants