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] CompareObjectsWithEqualsRule: False positive with Enums · Issue #2716 · pmd/pmd

[java] CompareObjectsWithEqualsRule: False positive with Enums #2716

New issue
Copy link
New issue
Copy link
Closed
#2899
Closed
[java] CompareObjectsWithEqualsRule: False positive with Enums#2716
#2899
Copy link
Labels
a:false-positivePMD flags a piece of code that is not problematic
Milestone
6.32.0

Description

@ghost
ghost
opened on Aug 19, 2020

Affects PMD Version:
6.26.0
Rule:
CompareObjectsWithEquals

Description:
With #885 this rule was fixed to not show a false-positive violation when comparing enums with '=='.
However there is still a way to get a false-positive.

Code Sample demonstrating the issue:

public class EnumTest {
	enum Type {
		A, B;
	}
	
	private final Type type = Type.A;
	
	public String isTypeA(Type param) {
		return param == type ? "Yes" : "No";
	}
}

Expected outcome:
No violation -> false-positive

Running PMD through: Eclipse

Activity

  1. ghost added
    a:bugPMD crashes or fails to analyse a file.
    on Aug 19, 2020
  2. oowekyala
    added
    a:false-negativePMD doesn't flag a problematic piece of code
    and removed
    a:bugPMD crashes or fails to analyse a file.
    on Aug 19, 2020
  3. adangel
    added
    a:false-positivePMD flags a piece of code that is not problematic
    and removed
    a:false-negativePMD doesn't flag a problematic piece of code
    on Aug 21, 2020
  4. oowekyala
    added this to the 7.0.0 milestone on Nov 5, 2020
  5. oowekyala
    added a commit that references this issue on Nov 5, 2020

    Check fixes pmd#2716

    This issue will close once commit 7c5fbb2 is merged into the 'main' branch.
    7c5fbb2
  6. oowekyala
    linked a pull request that will close this issue[java] Update many java rules #2899on Nov 10, 2020
  7. oowekyala
    removed a link to a pull request[java] Update many java rules #2899on Nov 10, 2020
  8. oowekyala
    linked a pull request that will close this issue[java] Update many java rules #2899on Nov 10, 2020
  9. adangel commented on Dec 11, 2020

    @adangel
    adangel
    on Dec 11, 2020
    Member
    More actions

    Fixed via #2899 for PMD 7.

  10. adangel
    closed this as completedon Dec 11, 2020
  11. uhafner commented on Feb 22, 2021

    @uhafner
    uhafner
    on Feb 22, 2021
    More actions

    I also get a lot of false positives for this rule when upgrading from 6.29.0 to 6.30.0 or 6.31.0:
    https://github.com/jenkinsci/warnings-ng-plugin/pull/774/checks?check_run_id=1804343680

    What are the plans for 7.0.0? The backlog looks quite large before we can expect a new major release.

  12. adangel commented on Feb 25, 2021

    @adangel
    adangel
    on Feb 25, 2021
    Member
    More actions

    What are the plans for 7.0.0? The backlog looks quite large before we can expect a new major release.

    That indeed will still take a while. But we are open to fix/backport issues that are already fixed for pmd 7, if there is a demand. We also accept pull requests.

    I've looked at this issue again and that's my conclusion:

    • This bug here is not reproducible with 6.26.0 - if PMD is used on a compiled project. Then we correctly detect the Enum and don't report it.
    • The OP mentioned, he ran PMD through Eclipse. It could be a pmd-eclipse-plugin specific issue, however: I could only reproduce the problem, if I analyse the project without compiling it.
    • When the project is not compiled, we actually figure out, that it is a enum since 6.30.0 and don't report it. So, basically, this issue is fixed since 6.30.0
    • I'll add a test case for this specific case (the same we use in pmd 7) - there is no specific fix for this issue - it already works.

    Then I looked at your false positives:
    There are 12 new violations found with "CompareObjectsWithEquals" and one new violations with "LiteralsFirstInComparisons".
    The CompareObjectsWithEquals are from two different files:

    • https://github.com/jenkinsci/warnings-ng-plugin/blob/master/plugin/src/main/java/io/jenkins/plugins/analysis/core/charts/SeverityPalette.java#L21-L30
    • https://github.com/jenkinsci/warnings-ng-plugin/blob/master/plugin/src/main/java/io/jenkins/plugins/analysis/core/util/LocalizedSeverity.java#L20-L29 and https://github.com/jenkinsci/warnings-ng-plugin/blob/master/plugin/src/main/java/io/jenkins/plugins/analysis/core/util/LocalizedSeverity.java#L44-L53
      Both use "==" to compare a ordinary instance. Severity is a class and not a enum. So, this violation is actually correct and not a false positive. The easiest way for you is, to suppress this rule, since it appears only in 3 methods: @SuppressWarnings("PMD.CompareObjectsWithEquals"). Or you could convert Severity into a real enum (it almost looks like a enum, but there is a public constructor...)

    The one new violation with "LiteralsFirstInComparisons" appears here: https://github.com/jenkinsci/warnings-ng-plugin/blob/9869a3bff1f20cfc593c2bfe01bacdc55c7dc625/plugin/src/test/java/io/jenkins/plugins/analysis/core/model/PropertyStatisticsTest.java#L94
    There you could indeed write string -> KEY.equals(string) ? KEY : "tooltip" and your lambda is null-safe.

  13. adangel
    added a commit that references this issue on Feb 25, 2021

    [java] CompareObjectsWithEqualsRule: False positive with Enums

    7146e58
  14. adangel
    modified the milestones: 7.0.0, 6.32.0 on Feb 25, 2021
  15. uhafner commented on Feb 25, 2021

    @uhafner
    uhafner
    on Feb 25, 2021
    More actions

    Thanks for clarifying! You are right, the class actually is not an enum it is just used in the same way. It seems that I forgot about it since I haven't looked into that part of my code since a long time...

  16. adangel
    added
    needs-backport
    on Feb 25, 2022
  17. oowekyala
    added a commit that references this issue on Jul 17, 2022

    Fix build

    8587844
  18. adangel
    added a commit that references this issue on Jan 13, 2023

    [doc] Update release notes [skip ci]

    1262034
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 problematic

    Type

    No type

    Projects

    No projects

      Milestone

      • 6.32.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.