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:
Description | Operator | Ranking (From High to Low) |
---|---|---|
Other | Any operator not listed in this table | 14 |
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 |
Throw | throw | 1 |
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
<<
, 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
andalignof
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
Check Information
Group: Expressions |
Category: Advisory |
Version History
Introduced in R2024b