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

Switch labels should not be nested inside non-switch blocks

    Details

    • Default Severity:
      Blocker
    • Impact:
      High
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Legacy Key:
      SwitchLabelPlacement
    • Covered Languages:
      C, C++, Objective-C
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      20min
    • MISRA C 2004:
      15.1
    • MISRA C 2012:
      16.2
    • MISRA C++ 2008:
      6-4-4
    • PC-Lint:
      9055

      Description

      A switch-label can be placed anywhere within the statements that form the body of a switch statement, potentially leading to unstructured code. To prevent this from happening, the scope of a case-label or default-label shall be the statement forming the body of a switch statement. All case-clauses and the default-clause shall be at the same scope.

      Noncompliant Code Example

      switch (x) {
        case 1: // Compliant
          if (foo) {
            case 2: // Noncompliant
              break;
            default: // Noncompliant
              break;
          }
          break;
        default: // Compliant
          break;
      }
      

      See

      • MISRA C 2004, 15.1 - A switch label shall only be used when the most closely-enclosing compound statement is the body of a switch statement.
      • MISRA C++ 2008, 6-4-4 - A switch-label shall only be used when the most closely-enclosing compound statement is the body of a switch statement.
      • MISRA C 2012, 16.2 - A switch label shall only be used when the most closely-enclsoing compound statement is the body of a switch statement

        Attachments

          Issue Links

          1.
          C-Family RSPEC-4371 Language-Specification Active Unassigned

            Activity

              People

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

                Dates

                • Created:
                  Updated: