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

Null checks should not be used with "instanceof"

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Remove this unnecessary null check; "instanceof" returns false for nulls.
    • Highlighting:
      Hide

      null check

      Show
      null check
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way
    • Covered Languages:
      C#, Java, VB.Net
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Scope:
      Main Sources, Test Sources

      Description

      There's no need to null test in conjunction with an instanceof test. null is not an instanceof anything, so a null check is redundant.

      Noncompliant Code Example

      if (x != null && x instanceof MyClass) { ... }  // Noncompliant
      
      if (x == null || ! x instanceof MyClass) { ... } // Noncompliant
      

      Compliant Solution

      if (x instanceof MyClass) { ... }
      
      if (! x instanceof MyClass) { ... }
      

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                • Created:
                  Updated: