Skip to content

[java] A false negative about the rule CompareObjectsWithEquals #3947

@ghost

Description

Affects PMD Version:6.42.0

Rule:CompareObjectsWithEquals

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

Description:
Hi, I found a FN about this rule. PMD should have reported a warning in line 5 because here a is compared by ==, not equals. However, line 8 can get a warning and they are equivalent. Hence, I think this is a false negative.

This issue is caused by the incorrect result of count(PrimaryExpression[pmd-java:typeIs('java.lang.Object')])=2 in defined XPath rule, typeIs fails to recognize b[get()] as java.lang.Object.

Code Sample demonstrating the issue:

int get() {
    return 1;
}
void array1(String[] a, String[] b) {
    if (a[1] == b[get()]) {} // should report a warning
}
void array2(String[] a, String[] b) {
    if (a[1] == b[1]) {} // can detect
}

Expected outcome:

PMD should report a violation at line 5, but doesn't. This is a false-negative.

Running PMD through: [Maven]

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-negativePMD doesn't flag a problematic piece of code

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions