Why is this an issue?
Conditional expressions which are always true
or false
can lead to dead code. Such code is always buggy and should never
be used in production.
Noncompliant code example
a = false;
if (a) { // Noncompliant
doSomething(); // never executed
}
if (!a || b) { // Noncompliant; "!a" is always "true", "b" is never evaluated
doSomething();
} else {
doSomethingElse(); // never executed
}
Resources
- MISRA C:2004, 13.7 - Boolean operations whose results are invariant shall not be permitted.
- MISRA C:2012, 14.3 - Controlling expressions shall not be invariant
- MITRE, CWE-570 - Expression is Always False
- MITRE, CWE-571 - Expression is Always True
- CERT, MSC12-C. - Detect and remove code that has no effect or is never executed