Rule Definition
The use of bool operands with other operators is unlikely to be meaningful (or intended). This rule allows the detection of such uses, which often occur because of the logical operators (&&, || and !) can be easily confused with the bitwise operators (&, | and ~).
Remediation
Don't use bool expression as operands to buit-in operators other than =, &&, ||, !, ==, !=, unary & and the conditional operator
Violation Code Sample
bool b1 = true;
bool b2 = false;
int8_t s8a;
if ( b1 & b2 ) // Non-compliant
if ( b1 < b2 ) // Non-compliant
if ( ~b1 ) // Non-compliant
if ( b1 ^ b2 ) // Non-compliant
Fixed Code Sample
if ( b1 == false ) // Compliant
if ( b1 == b2 ) // Compliant
if ( b1 != b2 ) // Compliant
if ( b1 && b2 ) // Compliant
if ( !b1 ) // Compliant
s8a = b1 ? 3 : 7; //Compliant
Reference
MISRA C++ 2008, Rule 4–5–1: Expressions with type bool shall not be used as operands to built-in operators other than the assignment operator =, the logical operators &&, ||, !, the equality operators == and !=, the unary & operator, and the conditional operator.
AUTOSAR, C++, 2014, Rule M4-5-1: Expressions with type bool shall not be used as operands to built-in operators other than the assignment operator =, the logical operators &&, ||, !, the equality operators == and ! =, the unary & operator, and the conditional operator.
Related Technologies
Technical Criterion
Programming Practices - Unexpected Behavior
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.