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

Conditions should not contain unnecessary null tests

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Hide
      * Remove the null-test of "xxx"; it is redundant.
      * Remove the null-test of "xxx" or use its ".equals" method instead of "yyy's".
      Show
      * Remove the null-test of "xxx"; it is redundant. * Remove the null-test of "xxx" or use its ".equals" method instead of "yyy's".
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way
    • Targeted languages:
      Java
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      2min
    • PMD:
      SimplifyConditional, UnusedNullCheckInEquals

      Description

      There's no need to null-test a variable before an instanceof test because instanceof tests for null. Similarly, there's no need to null-test a variable before dereferencing some other object.

      Noncompliant Code Example

      if (myVar != null && myVar instanceof MyClass) { // Noncompliant
        // ...
      } else if (myVar != null && myOtherVar.equals(myVar) {  // Noncompliant
        // ...
      }
      

      Compliant Solution

      if (myVar instanceof MyClass) {
        // ...
      } else if (myVar != null && myVar.equals(myOtherVar) {
        // ...
      }
      

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated: