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.NODEFAULT

No default clause at the end of a switch statement.

MISRA-C Rule 15.3 (required): The final clause of a switch statement shall be the default clause.

The requirement for a final default clause is defensive programming. This clause shall either take appropriate action or contain a suitable comment as to why no action is taken.

MISRA-C++ Rule 6-4-6 (required): The final clause of a switch statement shall be the default-clause.

Rationale

The requirement for a final default-clause is defensive programming. This clause shall either take appropriate action, or else contain a suitable comment as to why no action is taken.

Exception

If the condition of a switch statement is of type enum, and all the enumerators are listed in case labels, then the default-clause is not required as the rules associated with enums are intended to ensure that the enum cannot be assigned values outside of its set of enumerators. Note that it may still be appropriate to include a default-clause for the purpose of defensive programming.

Example

switch ( int16 )
{
case 0:
   break;
case 1:
case 2:
   break;
                 // Non-compliant — default clause is required.
}
enum Colours { RED, BLUE, GREEN } colour;

switch ( colour )
{
   case RED:
      break;
   case GREEN:
      break;
                 // Non-compliant — default clause is required.
}
switch ( colour )
{
   case RED:
      break;
   case BLUE:
      break;
   case GREEN:
      break;
                 // Compliant — exception allows no default in this case
}