Skip to content

[java] BrokenNullCheck FP with PMD 6.30.0 #3071

@testation21

Description

@testation21

Affects PMD Version: pmd 6.30.0

Rule: BrokenNullCheck
https://pmd.github.io/pmd-6.30.0/pmd_rules_java_errorprone.html#brokennullcheck

Description:
Hello, I think I found some False Positives in BrokenNullCheck and I want to report them.

There are some alarms on cases which don't call method on object. It means that they cannot throw NullPointException.
I think that I found three cases which are different from each other.
Case 1. Using instanceof
Case 2. Using Boolean Class
Case 3. Comparing between Local Variable and Field which have the same identifier and accessed by this.

Details about codes are below.
Thank you.

Code Sample demonstrating the issue:

class Test {
    Object obj = new Object();
    Object objectWithDifferentName;

    public void testPMD(){
        Object obj = new Object();
        String str = "str";
        Boolean boo = null;

        //Case 1. : An Alarm is generated
        if(obj == null && !(obj instanceof String));

        //Case 2. : Alarms are generated for each line.
        if(boo == null && boo != true) ;
        if(boo == null && boo != false);

        //Case 3.
        if(obj == null && this.obj == null); //An Alarm is generated
        if(obj == null && this.objectWithDifferentName); //An Alarm isn't generated
        if(obj == null && objectWithDifferentName); //An Alarm isn't generated
    }
 }

Expected outcome:
Expected that there wouldn't be any alarms.

Running PMD through: [CLI]

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions