Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.SWITCH.WELL_FORMED.DEFAULT.2012

Every switch statement shall have a default label.

MISRA C 2012 Rule 16.4: Every switch statement shall have a default label

Category: Required

Analysis: Decidable, Single Translation Unit

Applies to: C90, C99

Amplification

The switch-clause following the default label shall, prior to the terminating break statement, contain either:

  • A statement, or
  • A comment.

Rationale

The requirement for a default label is defensive programming. Any statements following the default label are intended to take some appropriate action. If no statements follow the label then the comment can be used to explain why no specific action has been taken.

Example

int16_t x;

switch ( x ) 
{
  case 0:
    ++x; 
    break; 
  case 1: 
  case 2:
    break;
                        /* Non-compliant - default label is required */ 
}

int16_t x;

switch ( x ) 
{
  case 0:
    ++x; 
    break; 
  case 1: 
  case 2:
    break; 
  default:              /* Compliant - default label is present      */ 
errorflag = 1;          /* should be non-empty if possible           */ 
break; 
}

enum Colours
{ RED, GREEN, BLUE } colour;

switch ( colour ) 
{
  case RED:
    next = GREEN; 
    break; 
  case GREEN:
    next = BLUE; 
    break; 
  case BLUE:
    next = RED; 
    break;
                          /* Non-compliant - no default label.
                           * Even though all values of the enumeration are 
                           * handled there is no guarantee that colour takes 
                           * one of those values                              */ 
}

See also

Rule 2.1, Rule 16.1