Skip to main content

IAR Embedded Workbench for RX 5.20

MISRAC2012-Rule-16.1

In this section:
Synopsis

(Required) All switch statements shall be well-formed

Enabled by default

Yes

Severity/Certainty

Low/High

lowhigh.png
Full description

Detected switch statements that do not conform to the MISRA C switch syntax. This check is identical to MISRAC2004-15.0.

Coding standards
MISRA C:2004 15.0

(Required) The MISRA C switch syntax shall be used.

MISRA C:2012 Rule-16.1

(Required) All switch statements shall be well-formed

MISRA C++ 2008 6-4-3

(Required) A switch statement shall be a well-formed switch statement.

Code examples

The following code example fails the check and will give a warning:

void example(void) {
	switch(expr()) {
		// at least one case label
		case 1:
			// statement list
			stmt();
			stmt();
			// WARNING: missing break at end of statement list
		default:
			break; // statement list ends in a break
	}

	switch(expr()) {
		// WARNING: missing at least one case label
		default:
			break; // statement list ends in a break
	}

	switch(expr()) {
		// at least one case label
		case 1:
			// statement list
			stmt();
			stmt();
			break; // statement list ends in a break
		case 0:
			stmt();
			// WARNING: declaration list without block
			int decl = 0;
			int x;
			// statement list
			stmt();
			stmt();
			break; // statement list ends in a break
		default:
			break; // statement list ends in a break
	}

	switch(expr()) {
		// at least one case label
		case 1: {
			// statement list
			stmt();
			// WARNING: Additional block inside of the case clause block
			{
			stmt();
			}
			break;
		}
		default:
			break; // statement list ends in a break
	}
}

The following code example passes the check and will not give a warning about this issue:

void example(void) {
	switch(expr()) {
		// at least one case label
		case 1:
			// statement list (no declarations)
			stmt();
			stmt();
			break; // statement list ends in a break
		case 0: {
			// one level of block is allowed
			// declaration list
			int decl = 0;
			// statement list
			stmt();
			stmt();
			break; // statement list ends in a break
		}
		case 2: // empty cases are allowed
		default:
			break; // statement list ends in a break
	}
}