OperatorWrapCheck throws NPE on guarded patterns (original) (raw)

Link to check documentation: https://checkstyle.sourceforge.io/config_whitespace.html#OperatorWrap

Noticed in #11186 at https://github.com/checkstyle/checkstyle/runs/4785736479?check_suite_focus=true :


➜  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 Test.java                                             
public class Test {
    String typeGuardAfterParenthesizedTrueSwitchStatement(Object o) {
        switch (o) {
            case (Integer i) && i == 0: o = String.valueOf(i); return "true";
            case ((Integer i) && i == 2): o = String.valueOf(i); return "second";
            case Object x: return "any";
        }
    }

    String typeGuardAfterParenthesizedTrueSwitchExpression(Object o) {
        return switch (o) {
            case (Integer i) && i == 0: o = String.valueOf(i); yield "true";
            case ((Integer i) && i == 2): o = String.valueOf(i); yield "second";
            case Object x: yield "any";
        };
    }

    String typeGuardAfterParenthesizedTrueIfStatement(Object o) {
        if (o != null && o instanceof ((Integer i) && i == 0)) {
            return "true";
        } else if (o != null && o instanceof (((Integer i) && i == 2)) && (o = i) != null) {
            return "second";
        } else {
            return "any";
        }
    }
}

➜  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="OperatorWrap">
        </module>
    </module>
</module>

➜  java -jar checkstyle-9.3-SNAPSHOT-all.jar -c config.xml Test.java
Starting audit...
com.puppycrawl.tools.checkstyle.api.CheckstyleException: Exception was thrown while processing Test.java
        at com.puppycrawl.tools.checkstyle.Checker.processFiles(Checker.java:305)
        at com.puppycrawl.tools.checkstyle.Checker.process(Checker.java:224)
        at com.puppycrawl.tools.checkstyle.Main.runCheckstyle(Main.java:407)
        at com.puppycrawl.tools.checkstyle.Main.runCli(Main.java:330)
        at com.puppycrawl.tools.checkstyle.Main.execute(Main.java:189)
        at com.puppycrawl.tools.checkstyle.Main.main(Main.java:126)
Caused by: java.lang.NullPointerException: Cannot invoke "com.puppycrawl.tools.checkstyle.api.DetailAST.getType()" because "result" is null
        at com.puppycrawl.tools.checkstyle.checks.whitespace.OperatorWrapCheck.adjustParens(OperatorWrapCheck.java:452)
        at com.puppycrawl.tools.checkstyle.checks.whitespace.OperatorWrapCheck.getLeftNode(OperatorWrapCheck.java:394)
        at com.puppycrawl.tools.checkstyle.checks.whitespace.OperatorWrapCheck.isNewLineModeViolation(OperatorWrapCheck.java:345)
        at com.puppycrawl.tools.checkstyle.checks.whitespace.OperatorWrapCheck.visitToken(OperatorWrapCheck.java:308)
        at com.puppycrawl.tools.checkstyle.TreeWalker.notifyVisit(TreeWalker.java:335)
        at com.puppycrawl.tools.checkstyle.TreeWalker.processIter(TreeWalker.java:406)
        at com.puppycrawl.tools.checkstyle.TreeWalker.walk(TreeWalker.java:273)
        at com.puppycrawl.tools.checkstyle.TreeWalker.processFiltered(TreeWalker.java:154)
        at com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck.process(AbstractFileSetCheck.java:87)
        at com.puppycrawl.tools.checkstyle.Checker.processFile(Checker.java:331)
        at com.puppycrawl.tools.checkstyle.Checker.processFiles(Checker.java:292)
        ... 5 more
Checkstyle ends with 1 errors.

I would expect OperatorWrapCheck to process Test.java without throwing a null pointer exception.

We can remove file filter for this file from openjdk17-excluded.files in PR for this issue, and exclusion from any project files in contribution.