|
| 1 | +/** |
| 2 | + * @id cpp/autosar/insufficient-use-of-parentheses |
| 3 | + * @name M5-0-2: Limited dependence should be placed on C++ operator precedence rules in expressions |
| 4 | + * @description The use of parentheses can be used to emphasize precedence and increase code |
| 5 | + * readability. |
| 6 | + * @kind problem |
| 7 | + * @precision medium |
| 8 | + * @problem.severity recommendation |
| 9 | + * @tags external/autosar/id/m5-0-2 |
| 10 | + * external/autosar/audit |
| 11 | + * readability |
| 12 | + * external/autosar/allocated-target/implementation |
| 13 | + * external/autosar/enforcement/partially-automated |
| 14 | + * external/autosar/obligation/advisory |
| 15 | + */ |
| 16 | + |
| 17 | +import cpp |
| 18 | +import codingstandards.cpp.autosar |
| 19 | +import semmle.code.cpp.commons.Assertions |
| 20 | + |
| 21 | +class InsufficientlyParenthesizedExpr extends Expr { |
| 22 | + InsufficientlyParenthesizedExpr() { |
| 23 | + // Exclude expressions affected by macros, including assertions, because |
| 24 | + // it is unclear that the expression must be parenthesized since it seems |
| 25 | + // to be the top-level expression instead of an operand of a binary or ternary operation. |
| 26 | + not this.isAffectedByMacro() and |
| 27 | + ( |
| 28 | + exists(BinaryOperation root, BinaryOperation child | child = this | |
| 29 | + root.getAnOperand() = child and |
| 30 | + root.getOperator() != child.getOperator() and |
| 31 | + not any(ParenthesisExpr pe).getExpr() = child |
| 32 | + ) |
| 33 | + or |
| 34 | + exists(ConditionalExpr root, BinaryOperation child | child = this | |
| 35 | + root.getAnOperand() = child and |
| 36 | + not any(ParenthesisExpr pe).getExpr() = child |
| 37 | + ) |
| 38 | + ) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +from InsufficientlyParenthesizedExpr e |
| 43 | +where not isExcluded(e, OrderOfEvaluationPackage::insufficientUseOfParenthesesQuery()) |
| 44 | +select e, "Dependence on operator precedence rules." |
0 commit comments