Main Content

MISRA C++:2008 Rule 6-2-3

Before preprocessing, a null statement shall only occur on a line by itself; it may be followed by a comment, provided that the first character following the null statement is a white-space character

Description

Rule Definition

Before preprocessing, a null statement shall only occur on a line by itself; it may be followed by a comment, provided that the first character following the null statement is a white-space character.

Rationale

A null statement:

;
used on a line with other code can indicate a programming error. If you want to enter a null statement, place the statement on a line by itself.

A comment can follow a null statement, but use a white space character between the null statement and the comment to provide a visual cue for reviewers.

Polyspace Implementation

The rule checker considers a null statement as a line where the first character excluding comments is a semicolon. The rule checker reports violations for situations where:

  • Comments appear before the null statement.

    For instance:

    /* wait for pin */ ; 

  • Comments appear immediately after the semicolon without a white space in between.

    For instance:

    ;// wait for pin

The rule checker also reports a violation when a second statement appears on the same line following the null statement.

For instance:

; count++;

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

In this example, there are three different versions of comments after a null statement.

  • The first option is compliant. The comment comes after the null statement, the null statement is the only statement on the line, and there is a white space separating the semicolon and the comment.

  • The second option is noncompliant. The comment comes before the null statement.

  • The third option is noncompliant. A white space does not separate the semicolon and comment.

#include <iostream>

void main()
{
	std::cout << "Hello World" << std::endl;
	 ; // comment here - Compliant     
	 /* comment here */ ; // Noncompliant
	 ;//comment here - Noncompliant
	
}

Check Information

Group: Statements
Category: Required

Version History

Introduced in R2013b