Skip to content

Commit e49c8b9

Browse files
committed
Exclude asserted expressions
The way assert is implemented will result in an alert pointing to the expression being asserted. This will result in a false positive, as the expression does not need to be parenthesized.
1 parent ef44566 commit e49c8b9

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

cpp/autosar/src/rules/M5-0-2/InsufficientUseOfParentheses.ql

+15-9
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,24 @@
1616

1717
import cpp
1818
import codingstandards.cpp.autosar
19+
import semmle.code.cpp.commons.Assertions
1920

2021
class InsufficientlyParenthesizedExpr extends Expr {
2122
InsufficientlyParenthesizedExpr() {
22-
exists(BinaryOperation root, BinaryOperation child | child = this |
23-
root.getAnOperand() = child and
24-
root.getOperator() != child.getOperator() and
25-
not any(ParenthesisExpr pe).getExpr() = child
26-
)
27-
or
28-
exists(ConditionalExpr root, BinaryOperation child | child = this |
29-
root.getAnOperand() = child and
30-
not any(ParenthesisExpr pe).getExpr() = child
23+
// Exclude assertions because if the child is the expression being asserted it
24+
// is not necessary to add parenthesis.
25+
not any(Assertion a).getAsserted() = this and
26+
(
27+
exists(BinaryOperation root, BinaryOperation child | child = this |
28+
root.getAnOperand() = child and
29+
root.getOperator() != child.getOperator() and
30+
not any(ParenthesisExpr pe).getExpr() = child
31+
)
32+
or
33+
exists(ConditionalExpr root, BinaryOperation child | child = this |
34+
root.getAnOperand() = child and
35+
not any(ParenthesisExpr pe).getExpr() = child
36+
)
3137
)
3238
}
3339
}

0 commit comments

Comments
 (0)