Main Content

MISRA C++:2023 Rule 8.0.1

Parentheses should be used to make the meaning of an expression appropriately explicit

Since R2024b

Description

Rule Definition

Parentheses should be used to make the meaning of an expression appropriately explicit.

Rationale

The C++ standard supports a large number of operators and their relative precedence is not intuitive. Expressions that contain more than one operator can have unexpected values. Use of parentheses clarifies operator bindings and clearly shows the intent behind each operation.

To clarify when the meaning of an expression is appropriately explicit, the MISRA C++:2023 standard proposes this ranking for C++ operators:

DescriptionOperatorRanking (From High to Low)
OtherAny operator not listed in this table14
Multiplicative* / %13
Additive+ -12
Bitwise shift<< >>11
Relational< > <= >=10
Equality== !=9
Bitwise AND&8
Bitwise XOR^7
Bitwise OR|6
Logical AND&&5
Logical OR||4
Conditional?:3
Assignment=, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=2
Throwthrow1
Comma,0

Using this table, you can calculate the ranking of an expression. The ranking of an expression is the ranking of the root operator of the parse tree of the expression, that is, the operation that is performed last in the expression. For example, consider this expression:

a - b << c + d
The root operator of this expression is <<, which has a ranking of 11. The operators in the operands a - b and c + d each have a ranking of 12. Because the root operator has a ranking of 11, the ranking of the entire expression is also 11.

Whether the meaning of an expression is appropriately explicit depends on its ranking. An expression with appropriately explicit meaning has at least one of these properties:

  • The expression has a ranking 0, 1, 2, or 14.

  • Each operand of the expression is parenthesized, or has a ranking of 14.

  • Each operand of an expression has a ranking less than or equal to that of the expression.

The preceding expression has a ranking of 11 while each of its operands has a ranking of 12. The expression does not parenthesize its operands. The meaning of this expression is not appropriately explicit. To comply with this rule, parenthesize the operands a - b and c + d.

Polyspace Implementation

The rule checker reports a violation if either of these conditions are true:

  • An operand of an expression has a higher ranking than the root operator of the expression but the operands are not parenthesized.

  • The operands of the sizeof and alignof operators do not use parentheses.

Violations of this rule is not reported for assignment operators, unary operators, subscripting operators, and comma operators.

Troubleshooting

If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

This code shows examples of compliant and noncompliant expressions.

typedef int int32_t;
extern int func(int,int);
class myClass{
    public:
    double A;
    int B;
};
void foo(myClass* cPtr[], int32_t iter, int32_t *myPtr, int32_t num1, int32_t num2) {

	cPtr[ iter ]->A; // Compliant
	*myPtr++;  // Compliant
	sizeof num1 + num2;   // Noncompliant
}

void bar(int32_t intA, int32_t intB, int32_t intC, int32_t intD) {


	intA + intB - intC + intD;                        // Compliant
	(intA + intB) - (intC + intD);                    // Compliant

	int x = func(intA + intB, intC);                      // Compliant
	x = intA == intB ? intA : intA - intB;               // Noncompliant

	x = (intA == intB) ? intA : (intA - intB);      // Compliant

	x = intA << intB + intC;                    // Noncompliant

	intA && intB && intC;            //   Compliant
	intA || intB && intC;            //   Noncompliant
	intA && intB || intC;            //   Noncompliant
	intA || intB || intC;            //   Compliant
}
  • The operand of the sizeof operator is not parenthesized in foo(). Polyspace reports a violation. The other expressions in foo() use unary or postfix operators, which do not require parentheses to clarify their meanings.

  • The expression intA == intB ? intA : intA - intB uses the conditional operator ?: as a root operator, resulting in a ranking of 3. The subexpressions intA == intB and intA - intB of this expression have higher rankings but are not parenthesized. Polyspace reports a violation. The compliant expression is (intA == intB) ? intA : (intA - intB).

  • The expressions intA && intB || intC and intA || intB && intC are also reported as violations because a subexpression that has a higher ranking than the expression that includes it is not enclosed in parentheses.

Check Information

Group: Expressions
Category: Advisory

Version History

Introduced in R2024b