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.PRIMARY

Operand in a logical 'and' or 'or' expression is not a primary expression.

MISRA-C Rule 12.5 (required): The operands of a logical && or || shall be primary-expressions.

"Primary expressions" are defined in ISO/IEC 9899:1990 [2], section 6.3.1. Essentially they are either a single identifier, or a constant, or a parenthesised expression. The effect of this rule is to require that if an operand is other than a single identifier or constant then it must be parenthesised. Parentheses are important in this situation both for readability of code and for ensuring that the behaviour is as the programmer intended. Where an expression consists of either a sequence of only logical && or a sequence of only logical ||, extra parentheses are not required.

Example

if ( ( x == 0 ) && ishigh )   /* make x == 0 primary       */
if ( x || y || z )            /* exception allowed,
                                 if x, y and z are Boolean */
if ( x || ( y && z ) )        /* make y && z primary       */
if ( x && ( !y ) )            /* make !y primary           */
if ( ( is_odd (y) ) && x )    /* make call primary         */

Where an expression consists of either a sequence of only logical && or a sequence of only logical ||, extra parentheses are not required.

if ( ( x > c1) && (y > c2) && (z > c3) )   /* Compliant               */
if ( ( x > c1) && (y > c2) || (z > c3) )   /* not compliant           */
if ( ( x > c1) && ((y > c2) || (z > c3)) ) /* Compliant extra () used */

Note that this rule is a special case of Rule 12.1.