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

1   bool func()
2   {
3     const bool var = false;
4     const bool Constant1 = true;
5     const bool Constant2 = false;
6     return Constant1 || Constant2;  
7   }

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

1   bool func()
2   {
3     const bool var = false;
4     const bool Constant1 = true;
5     const bool Constant2 = false;
6     return var == Constant1 || var == Constant2;  
7   }

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