Main Content

AUTOSAR C++14 Rule A6-4-1

A switch statement shall have at least two case-clauses, distinct from the default label

Description

Rule Definition

A switch statement shall have at least two case-clauses, distinct from the default label.

Rationale

A switch statement with no case-clauses is redundant. Use an if statement to better express a switch statement with a single case-clause.

Polyspace Implementation

This rule checker reports a violation for switch statements that contain less than two case-clauses in addition to the default-clause. This includes situations where no case-clause exists.

	switch (x) {	//Noncompliant         
		default:
                   //...            
			break;
	}

Troubleshooting

If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

Because this switch statement contains a single case-clause alongside the default-clause, Polyspace flags the switch statement as noncompliant.

void example(int x)
{
	switch (x) {	//Noncompliant
		case 0:
			break;          
		default:            
			break;
	}
}

You can express the same code in an if statement form to avoid a rule violation.

void example(int x)
{
	if (x == 0)
	{
		//...
	}
	else
	{
		//...
	}
}

Check Information

Group: Statements
Category: Required, Automated

Version History

Introduced in R2019a