Boolean expression can be simplified
ID |
c.maintainability.simplify_boolean_expression |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Redundancy |
Language |
C / C++ |
Description
This boolean expression is more verbose than needed. Comparing to true/false (x == true) or returning boolean literals from both branches of an if/else is redundant. Simplify: x == true → x, x == false → !x, and if (c) return true; else return false; → return c;.
Rationale
This boolean expression is more verbose than needed. Comparing to true/false (x == true) or returning boolean literals from both branches of an if/else is redundant. Simplify: x == true → x, x == false → !x, and if (c) return true; else return false; → return c;.
The following code illustrates the pattern detected by this rule:
#include <stdbool.h>
bool check(int x, bool b) {
// FLAGGED: Boolean expression can be simplified
if (b == true) {
return b;
}
// FLAGGED: Boolean expression can be simplified
if (x == false) {
return b;
}
// FLAGGED: Boolean expression can be simplified
if (x > 0)
return true;
else
return false;
return b;
}