Why is this an issue?
Empty statements represented by a semicolon ;
are statements that do not perform any operation. They are often the result of a typo or
a misunderstanding of the language syntax. It is a good practice to remove empty statements since they don’t add value and lead to confusion and
errors.
Exceptions
In the case of an empty expanded macro, the issue is not raised.
#define LOG(x)
void fun() {
LOG(X);
}
Code examples
Noncompliant code example
void doSomething() {
; // Noncompliant - was used as a kind of TODO marker
}
#define A(x) x; // Noncompliant - macro definitions should not end with a semi-colon when they are used as functions
void fun() {
A(5); // Noncompliant - after expansion, there are 2 consecutive semi-colons
}
Compliant solution
void doSomething() {
}
#define A(x) x
void fun() {
A(5);
}
Resources
Documentation
- MISRA C:2004, 14.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.
- MISRA C++:2008, 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.
- CERT, MSC12-C. - Detect and remove code that has no effect or is never executed
- CERT, EXP15-C. - Do not place a semicolon on the same line as an if, for, or while
statement