CXX.LOGICAL_OP.NON_BOOL_CONSTANT

Logical operators should not be applied directly to constants.

This checker reports defects when a logical operator is applied directly to constants.

Vulnerability and risk

Performing logical operators on constants can lead to unexpected results.

Vulnerable code example

Copy
   bool func()
   {
     const bool var = false;
     const bool Constant1 = true;
     const bool Constant2 = false;
     return Constant1 || Constant2;  
   }

In the above example, line 6 is noncompliant because a logical operator is applied to the const variables. Klocwork produces a CXX.LOGICAL_OP.NON_BOOL_CONSTANT defect at line 6, indicating, "Logical operators should not be applied directly to constants."

Fixed code example

Copy
   bool func()
   {
     const bool var = false;
     const bool Constant1 = true;
     const bool Constant2 = false;
     return var == Constant1 || var == Constant2;  
   }

In the fixed example, Klocwork no longer reports the defect CXX.LOGICAL_OP.NON_BOOL_CONSTANT at line 6.