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

OperatorWrapCheck throws NPE on guarded patterns #11187

Closed
nrmancuso opened this issue Jan 12, 2022 · 1 comment · Fixed by #11188
Closed

OperatorWrapCheck throws NPE on guarded patterns #11187

nrmancuso opened this issue Jan 12, 2022 · 1 comment · Fixed by #11188
Assignees
Milestone

Comments

@nrmancuso
Copy link
Member

nrmancuso commented Jan 12, 2022

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.

@nrmancuso nrmancuso self-assigned this Jan 12, 2022
@nrmancuso nrmancuso added the bug label Jan 12, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jan 12, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jan 12, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jan 13, 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 21, 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 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 24, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jan 27, 2022
github-actions bot pushed a commit to nrmancuso/checkstyle that referenced this issue Jan 27, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jan 27, 2022
nrmancuso added a commit to nrmancuso/checkstyle that referenced this issue Jan 27, 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

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

Successfully merging a pull request may close this issue.

3 participants