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.EXPR.PARENS.REDUNDANT

Limited dependence required for operator precedence rules in expressions. Redundant use of parenthesis in an expression.

MISRA-C Rule 12.1 (advisory): Limited dependence should be placed on C's operator precedence rules in expressions.

In addition to the use of parentheses to override default operator precedence, parentheses should also be used to emphasize it. It is easy to make a mistake with the rather complicated precedence rules of C, and this approach helps to avoid such errors, and helps to make the code easier to read. However, do not add too many parentheses so as to clutter the code and make it unreadable.

The following guidelines are suggested in deciding when parentheses are required:

  • no parentheses are required for the right-hand operand of an assignment operator unless the right-hand side itself contains an assignment expression:
x = a + b;                 /* acceptable                  */
x = (a + b);               /* () not required             */
  • no parentheses are required for the operand of a unary operator:
x = a * -1;                /* acceptable                  */
x = a * (-1);              /* () not required             */
  • otherwise, the operands of binary and ternary operators shall be cast-expressions (see section 6.3.4 of ISO/IEC 9899:1990 [2]) unless all the operators in the expression are the same.
x = a + b + c;             /* acceptable, but care needed */
x = f (a + b, c);          /* no () required for a + b    */
x = (a == b) ? a : (a - b);
if (a && b && c)           /* acceptable                  */
x = (a + b) - (c + d);
x = (a * 3) + c + d;
x = (uint16_t) a + b;      /* no need for ((uint16_t) a)  */
  • even if all operators are the same, parentheses may be used to control the order of operation. Some operators (e.g. addition and multiplication) that are associative in algebra are not necessarily associative in C. Similarly, integer operations involving mixed types (prohibited by several rules) may produce different results because of the integral promotions. The following example written for a 16-bit implementation demonstrates that addition is not associative and that it is important to be clear about the structure of an expression:
uint16_t a = 10U;
uint16_t b = 65535U;
uint32_t c = 0U;
uint32_t d;

d = (a + b) + c;    /* d is 9; a + b wraps modulo 65536 */
d = a + (b + c);    /* d is 65545                       */
/* this example also deviates from several other rules  */

Note that Rule 12.5 is a special case of this rule applicable solely to the logical operators, '&&' and '||'.

MISRA-C++ Rule 5-0-2 (advisory): Limited dependence should be placed on C++ operator precedence rules in expressions.

Rationale

In addition to the use of parentheses to override default operator precedence, parentheses should also be used to emphasize it. It is easy to make a mistake with the rather complicated precedence rules of C++, and this approach helps to avoid such errors, and helps to make the code easier to read. However, too many parentheses can clutter the code and make it unreadable.

Example

The following guidelines are suggested for deciding when parentheses are required:

  • Parentheses are not required for the right-hand operand of an assignment operator unless the right-hand side itself contains an assignment expression:
x = a + b;    // acceptable
x = (a + b);  // () not required
  • Parentheses are not required for the operand of a unary operator:
x = a * -1;   // acceptable
x = a * (-1); // () not required

Otherwise, the operands of binary and ternary operators shall be cast-expressions (see Section 5.4(2) of ISO/IEC 14882:2003 [1]) unless all the operators in the expression are the same.

x = a + b + c;                   // acceptable, but care needed
x = f ( a + b, c );              // no () required for a + b
x = ( a == b ) ? a : ( a — b );
if ( a && b && c )               // acceptable
x = ( a + b ) — ( c + d );
x = ( a * 3 ) + c + d;
x = static_cast< uint16_t > ( a ) + b; // no need for cast
  • Even if all operators are the same, parentheses may be used to control the order of operation. Some operators (e.g. addition and multiplication) that are associative in algebra are not necessarily associative in C++. Similarly, integer operations involving mixed types (prohibited by several rules) may produce different results because of the integral promotions. The following example written for a 16-bit implementation demonstrates that addition is not associative and that it is important to be clear about the structure of an expression:
uint16_t a = 10U;
uint16_t b = 65535U;
uint32_t c = 0U;
uint32_t d;

d = (a + b) + c;   // d is 9; a + b wraps modulo 65536
d = a + (b + c);   // d is 65545
// this example also deviates from several other rules

Note that Rule 5—2—1 is a special case of this rule applicable solely to the logical operators, '&&' and '||'.