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

Switch cases should end with an unconditional "break" statement

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      End this switch case with an unconditional break, continue, return or throw statement.
    • Default Severity:
      Blocker
    • Impact:
      High
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Legacy Key:
      non-empty-case-without-break, NonEmptyCaseWithoutBreak
    • Covered Languages:
      C, C++, Flex, Java, JavaScript, Objective-C, PHP
    • Irrelevant for Languages:
      C#, PL/SQL, Python, Swift
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      10min
    • Analysis Scope:
      Main Sources, Test Sources
    • CERT:
      MSC17-C., MSC52-J.
    • CWE:
      CWE-484
    • Checkstyle:
      FallThrough
    • CPPCheck:
      switchCaseFallThrough
    • ESLint:
      no-fallthrough
    • FindBugs:
      SF_SWITCH_FALLTHROUGH
    • PC-Lint:
      616, 1616, 825, 1825, 9077, 9090
    • PMD:
      MissingBreakInSwitch

      Description

      When the execution is not explicitly terminated at the end of a switch case, it continues to execute the statements of the following case. While this is sometimes intentional, it often is a mistake which leads to unexpected behavior.

      Noncompliant Code Example

      switch (myVariable) {
        case 1:                              
          foo();
          break;
        case 2:  // Both 'doSomething()' and 'doSomethingElse()' will be executed. Is it on purpose ?
          doSomething();
        default:                               
          doSomethingElse();
          break;
      }
      

      Compliant Solution

      switch (myVariable) {
        case 1:                              
          foo();
          break;
        case 2: 
          doSomething();
          break;
        default:                               
          doSomethingElse();
          break;
      }
      

      Exceptions

      This rule is relaxed in the following cases:

      switch (myVariable) { 
        case 0: // Empty case used to specify the same behavior for a group of cases. 
        case 1: 
          doSomething(); 
          break; 
        case 2: // Use of return statement 
          return; 
        case 3: // Use of throw statement 
          throw new IllegalStateException(); 
        case 4: // Use of continue statement 
          continue; 
        default: // For the last case, use of break statement is optional 
          doSomethingElse(); 
      } 
      

      See

      • MITRE, CWE-484 - Omitted Break Statement in Switch
      • CERT, MSC17-C. - Finish every set of statements associated with a case label with a break statement
      • CERT, MSC52-J. - Finish every set of statements associated with a case label with a break statement

        Attachments

          Issue Links

          1.
          PHP RSPEC-1809 Language-Specification Active Unassigned
          2.
          JavaScript RSPEC-2866 Language-Specification Active Unassigned
          3.
          C-Family RSPEC-2893 Language-Specification Active Unassigned
          4.
          Java RSPEC-3108 Language-Specification Active Unassigned

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                freddy.mallet Freddy Mallet (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated: