Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.LOGIC.OPERATOR.NOT_BOOL

Operand of non-logical operator is effectively boolean.

MISRA-C Rule 12.6 (advisory): The operands of logical operators (&&, || and !) should be effectively Boolean. Expressions that are effectively Boolean should not be used as operands to operators other than (&&, ||, !, =, ==, != and ?:).

This rule is also covered by MISRA.LOGIC.OPERAND.NOT BOOL.

[Koenig 48]

The logical operators &&, || and ! can be easily confused with the bitwise operators &, | and ~. See "Boolean Expressions" in the glossary.

MISRA-C++ Rule 4-5-1 (required): 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.

Rationale

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 the logical operators (&&, || and !) can be easily confused with the bitwise operators (&, | and ~).

Example

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
if ( b1 == false )  // Compliant
if ( b1 == b2 )     // Compliant
if ( b1 != b2 )     // Compliant
if ( b1 && b2 )     // Compliant
if ( !b1 )          // Compliant
s8a = b1 ? 3 : 7;   // Compliant