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] New Rule: Always compare enum values with `==` · Issue #6193 · pmd/pmd

[java] New Rule: Always compare enum values with == #6193

New issue
Copy link
New issue
Copy link
Closed
#6196
Closed
[java] New Rule: Always compare enum values with ==#6193
#6196
Copy link
Labels
a:new-ruleProposal to add a new built-in rule
Milestone
7.19.0

Description

@adangel
adangel
opened on Oct 30, 2025
Issue body actions

Proposed Rule Name: EnumComparison

Proposed Category: Best Practices

Description:
See #6116 (comment)

Enum should be compared using ==, this is more readable. Using equals() is not necessary (and is prone to NPE).

Code Sample:

class Foo {
  void method(Thread.State state1, Thread.State state2) {
    if (state1.equals(state2)) { }  // violation

    if (state1 == state2) {}  // correct
  }
}

Possible XPath expression:

//MethodCall[pmd-java:matchesSig("_#equals(java.lang.Object)")]
  [VariableAccess[pmd-java:typeIs("java.lang.Enum")]]
  [ArgumentList[VariableAccess[pmd-java:typeIs("java.lang.Enum")]]]

Possible Properties:

  • none

References:

  • https://sonarsource.github.io/rspec/#/rspec/S4551
  • https://www.infoworld.com/article/2163724/use-or-to-compare-java-enums.html
  • https://stackoverflow.com/questions/1750435/comparing-java-enum-members-or-equals

Activity

  1. adangel
    added
    a:new-ruleProposal to add a new built-in rule
    on Oct 30, 2025
  2. adangel
    added this to the 7.19.0 milestone on Nov 6, 2025
  3. adangel
    added a commit that references this issue on Nov 6, 2025

    Add EnumComparison

    6a7a9be
  4. adangel
    closed this as completedin #6196on Nov 27, 2025
  5. adangel
    added 2 commits that reference this issue on Nov 27, 2025

    [doc] Update release notes (pmd#6193, pmd#6196)

    e5b80f3

    Add EnumComparison (#114)

    b244b5a
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:new-ruleProposal to add a new built-in rule

    Type

    No type

    Projects

    No projects

      Milestone

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