Skip to content

[java] False positives in UnusedNullCheckInEquals #3432

@Noah0120

Description

@Noah0120

Affects PMD Version:
6.36.0

Rule:
UnusedNullCheckInEquals
Please provide the rule name and a link to the rule documentation:
https://pmd.github.io/latest/pmd_rules_java_errorprone.html#unusednullcheckinequals

Description:
There are five cases that generate false positives:

  1. Using Arrays.equals()

  2. Using Bytes.equals()

    • The first and second cases don't use null checks.
  3. Using overridden equals() that has more than two arguments

    • This case also doesn't need null checks for comparanda.
  4. .class.getName() doesn't return null

  5. null checked in condition of conditional expression

    • After null checked in condition part of conditional expression,
      PMD generates alarms.

Code Sample demonstrating the issue:

class UnusedNullCheckInEquals{
        void test(){
                //case1: Arrays.equals()
                if (other.name != null && java.util.Arrays.equals(other.name,name));
                //case2: Bytes.equals() 
                if (startRow != null && startRow.length > 0 && Bytes.equals(startRow,stopRow));

                //case3: oveerriden equals() that has more than two arguments
                if (k1 == k2 || (k1 != null && hashingStrategy.equals(k1, k2)));
                if (passthroughNsPrefix != null && 
                LexerUtils.equals(passthroughNsPrefix, attribute.namespacePrefix(), true, true));

                //case4: .class.getName() cannot return null
                if (driverDelegate != null && 
                StdJDBCDelegate.class.getName().equals(driverDelegate));
                
                //case5: null checked in condition of conditional expression
                if (this.start == other.start && this.length == other.length && 
                (this.file == null ? 
                 other.file == null : (other.file != null && this.file.equals(other.file))));    
        }
}

Expected outcome:
PMD shouldn't report anything. These are false positives.

Thank you for your consideration.

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

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions