Skip to content

[java] UseEqualsToCompareStrings: FP with "var" variables #2979

@kris-scheibe

Description

@kris-scheibe

Affects PMD Version: 6.30.0

Rule: UseEqualsToCompareStrings

Description:
The rule detects a violation even though not two String variables but the results of the ".isEmpty()" method calls are compared.

This might be related to #2976 as CompareObjectsWithEquals falsely detects this as well.

Neither of the violations are reported if the variables are declared as "String" instead of "var".

Code Sample demonstrating the issue:

final Matcher matcher = ...;

if (matcher.matches()) {

final var firstString = matcher.group("a");
final var secondString = matcher.group("b");

if (firstString.isEmpty() != secondString.isEmpty()) { // <- violation
    // ...
}

}

Expected outcome:

  • The compared values are boolean, not Strings -> false-positive

Running PMD through: Other

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematicin:type-resolutionAffects the type resolution code

    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