GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] UseEqualsToCompareStrings: FP with "var" variables · Issue #2979 · pmd/pmd

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

New issue
Copy link
New issue
Copy link
Closed
#3061
Closed
[java] UseEqualsToCompareStrings: FP with "var" variables#2979
#3061
Copy link
Labels
a:false-positivePMD flags a piece of code that is not problematicin:type-resolutionAffects the type resolution code
Milestone
6.31.0

Description

@kris-scheibe
kris-scheibe
opened on Dec 16, 2020
Issue body actions

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

Activity

  1. kris-scheibe
    added
    a:bugPMD crashes or fails to analyse a file.
    on Dec 16, 2020
  2. oowekyala commented on Dec 16, 2020

    @oowekyala
    oowekyala
    on Dec 16, 2020
    Member
    More actions

    The type of those variables is inferred to Matcher for some reason...

  3. oowekyala
    added
    in:type-resolutionAffects the type resolution code
    on Dec 16, 2020
  4. adangel
    added
    a:false-positivePMD flags a piece of code that is not problematic
    and removed
    a:bugPMD crashes or fails to analyse a file.
    on Dec 17, 2020
  5. adangel
    changed the title [-][java] FP in UseEqualsToCompareStrings with "var" variables[/-] [+][java] UseEqualsToCompareStrings: FP with "var" variables[/+] on Dec 17, 2020
  6. catellie commented on Jan 7, 2021

    @catellie
    catellie
    on Jan 7, 2021
    More actions

    I think I've just seen a variant of this when I tried to upgrade to 6.30:

    Suddenly I see false positives for code like this:

    `
    String a = "";
    String b = "";
    if (a.length() == b.length() { // <-- this line flags UseEqualsToCompareStrings
    }

    `

  7. oowekyala
    linked a pull request that will close this issue[java] Fix method calls in type resolution #3061on Jan 16, 2021
  8. oowekyala
    added this to the 6.31.0 milestone on Jan 16, 2021
  9. adangel
    added a commit that references this issue on Jan 21, 2021

    [doc] Update release notes

    1050bce
  10. adangel
    closed this as completedin #3061on Jan 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

    Projects

    No projects

      Milestone

      • 6.31.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.