Menu

#1028 False-positive: Compare objects with equals for Enums

PMD-5.0.2
closed
pmd (543)
5
2013-01-26
2012-09-18
Anonymous
No

In the following code snippet, the last line leads to the mentioned violation:
ProcessingStatus currentCompletionStatus = detectCurrentCompletionStatus(opContent,isForOldCcbId);
ProcessingStatus newCompletionStatus = calculateNewCompletionStatus(transactionResult,currentCompletionStatus);
if (newCompletionStatus == currentCompletionStatus) { // <-- Violation!

But ProcessingStatus is an enum:
public enum ProcessingStatus {

The comparison of two enums with the identity check should be fine. Indeed it's the same what Enum.equals() does!
Still, I'm wondering why I didn't stumble upon this error earlier. Maybe this false-positive is not thrown for every enumeration comparison?

Discussion

  • Andreas Dangel

    Andreas Dangel - 2013-01-24
    • status: open --> accepted
    • assigned_to: Tom Copeland --> Andreas Dangel
    • milestone: --> PMD-5.0.2
     
  • Andreas Dangel

    Andreas Dangel - 2013-01-24

    That one is a bit tricky, as in order to determine, whether a type is a Enum, we need type resolution.

    I was able to adjust the rule to check, whether the type of the variables is an Enum. This works only, if the Enum types are on the "auxclasspath" of pmd, so that the type resolution can find it.

    Your example in isolation would still trigger this false positive, as PMD doesn't know what ProcessingStatus is.
    I verified it with java.math.RoundingMode, which is always on the classpath and will be resolved.

    I'd suggest to close this bug and if it doesn't work for you, simply create a new bug.

     
  • Andreas Dangel

    Andreas Dangel - 2013-01-26
    • Status: accepted --> closed
     

Log in to post a comment.