Statements with no side effects and no change of control flow do not contribute to the functionality of the code and can indicate a programming
error.
Why is this an issue?
When writing code, it is important to ensure that each statement serves a purpose and contributes to the overall functionality of the program. When
they have no side effects or do not change the control flow, they can either indicate a programming error or be redundant:
- The code does not behave as intended: The statements are expected to have an effect but they do not. This can be caused by mistyping,
copy-and-paste errors, etc.
- The statements are residual after a refactoring.
Exceptions
The rule does not raise an issue on statements containing only a semicolon (;
).
How to fix it
Identify statements that do not contribute to the functionality of the code and verify if they are intended to be part of the logic. If they are,
there is a bug to be fixed. If they are not, then they are redundant and should be removed.
Code examples
Noncompliant code example
int add(int a, int b) {
int result = 0;
a + b; // Noncompliant: no side effect, hides a bug, the developer likely accidentally duplicated the line
return result;
}
int mul(int a, int b) {
int result = 1;
result == a; // Noncompliant: no side effect, hides a bug, the developer intended to assign
result *= b;
return result;
}
int sub(int a, int b) {
int result = a - b;
a - b; // Noncompliant: no side effect, there is no underlying bug, but the statement is useless
return result;
}
Compliant solution
int add(int a, int b) {
int result = a + b;
return result;
}
int mul(int a, int b) {
int result = 1;
result = a;
result *= b;
return result;
}
int sub(int a, int b) {
int result = a - b;
return result;
}
Resources
Standards
External coding guidelines
- MISRA C:2004, 14.2 - All non-null statements shall either have at least one side-effect however executed or cause control flow to change.
Related rules
- S1116 - Empty statements should be removed
- S1854 - Unused assignments should be removed