RED-cmp-always
In this section:
Synopsis
A comparison using ==, <, <=, >, or >= is always true.
Enabled by default
No
Severity/Certainty
Low/Medium

Full description
A comparison using ==, <, <=, >, or >= is always true, given the values of the arguments of the comparison operator. This often occurs because literal values or macros have been used on one or both sides of the operator. Double-check that the operands and the code logic are correct. This check is identical to MISRAC2004-13.7_a.
Coding standards
- CWE 571
Expression is Always True
- MISRA C:2004 13.7
(Required) Boolean operations whose results are invariant shall not be permitted.
Code examples
The following code example fails the check and will give a warning:
int example(void) {
int x = 42;
if (x == 42) { //always true
return 0;
}
return 1;
}
The following code example passes the check and will not give a warning about this issue:
int example(void) {
int x = 42;
if (rand()) {
x = 40;
}
if (x == 42) { //OK - may not be true
return 0;
}
return 1;
}