Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-2200

"compareTo" results should not be checked for specific values

    XMLWordPrintable

    Details

    • Type: Bug Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Only the sign of the result should be examined.
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way
    • Covered Languages:
      Java
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Scope:
      Main Sources
    • FindBugs:
      RV_CHECK_COMPARETO_FOR_SPECIFIC_RETURN_VALUE

      Description

      While most compareTo methods return -1, 0, or 1, some do not, and testing the result of a compareTo against a specific value other than 0 could result in false negatives.

      Noncompliant Code Example

      if (myClass.compareTo(arg) == -1) {  // Noncompliant
        // ...
      }
      

      Compliant Solution

      if (myClass.compareTo(arg) < 0) {
        // ...
      }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              ann.campbell.2 Ann Campbell
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: