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

There should not be unreachable code

    Details

    • Type: Bug Detection
    • Status: Closed
    • Resolution: Duplicate
    • Labels:
    • Message:
      Remove this unreachable statement.
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Targeted languages:
      C, C++
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      10min
    • MISRA C 2004:
      14.1
    • MISRA C 2012:
      2.1
    • MISRA C++ 2008:
      0-1-1

      Description

      Code is unreachable if there is no syntactic (control flow) path to it. If such code exists, it is unclear if this is intentional or simply that an appropriate path has been accidentally omitted.
      Compilers may choose not to generate code for these constructs, meaning that, even if the unreachable code is intentional, it may not be present in the final executable code.
      Missing statements, often caused by editing activities, are a common source of unreachable code.

      Noncompliant Code Example

      int16_t with_unreach ( int16_t para )
      {
       int16_t local;
       local = 0;
       switch ( para )
       {
          local = para; // unreachable – Noncompliant
        case 1:
          break;
          para++; // unreachable – Noncompliant
        default:
          break;
        }
        return para;
        para++; // unreachable – Noncompliant
      }
      

      Compliant Solution

      int16_t with_unreach ( int16_t para )
      {
       int16_t local;
       local = 0;
       switch ( para )
       {
        case 1:
          break;
        default:
          break;
        }
        return para;
      }
      

      See

      • MISRA C:2004, 14.1
      • MISRA C++:2008, 0-1-1
      • MISRA C:2012, 2.1

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                Anonymous
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: