Skip to content

A5-2-6: Ignore cases with the same operator #753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions change_notes/2024-10-17-a5-2-6-no-ambiguity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- `A5-2-6` - `OperandsOfAlogicalAndOrNotParenthesized.ql`:
- Remove false positives where the operator is identical.
- Improve alert message to clarify which expression needs to be parenthesized.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,20 @@
import cpp
import codingstandards.cpp.autosar

from BinaryLogicalOperation op, BinaryOperation binop
from BinaryLogicalOperation op, BinaryOperation binop, string leftOrRight
where
not isExcluded(op, OrderOfEvaluationPackage::operandsOfALogicalAndOrNotParenthesizedQuery()) and
op.getAnOperand() = binop and
(
op.getLeftOperand() = binop and
leftOrRight = "Left"
or
op.getRightOperand() = binop and
leftOrRight = "Right"
) and
// Ignore cases with the same operator
not op.getOperator() = binop.getOperator() and
not exists(ParenthesisExpr p | p = binop.getFullyConverted()) and
// Exclude binary operations expanded by a macro.
not binop.isInMacroExpansion()
select op, "Binary $@ operand of logical operation is not parenthesized.", binop, "operator"
select op, "$@ of logical operation " + op.getOperator() + " is not parenthesized.", binop,
leftOrRight + " operand " + binop.getOperator()
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
| test.cpp:3:7:3:23 | ... && ... | Binary $@ operand of logical operation is not parenthesized. | test.cpp:3:7:3:12 | ... > ... | operator |
| test.cpp:3:7:3:23 | ... && ... | Binary $@ operand of logical operation is not parenthesized. | test.cpp:3:17:3:23 | ... < ... | operator |
| test.cpp:7:7:7:24 | ... \|\| ... | Binary $@ operand of logical operation is not parenthesized. | test.cpp:7:19:7:24 | ... > ... | operator |
| test.cpp:3:7:3:23 | ... && ... | $@ of logical operation && is not parenthesized. | test.cpp:3:7:3:12 | ... > ... | Left operand > |
| test.cpp:3:7:3:23 | ... && ... | $@ of logical operation && is not parenthesized. | test.cpp:3:17:3:23 | ... < ... | Right operand < |
| test.cpp:7:7:7:24 | ... \|\| ... | $@ of logical operation \|\| is not parenthesized. | test.cpp:7:19:7:24 | ... > ... | Right operand > |
2 changes: 2 additions & 0 deletions cpp/autosar/test/rules/A5-2-6/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ void f2(int p1, int p2) {
f1();
}

(p1 > 0) && (p2 > 0) && (p1 > p2); // COMPLIANT - no ambiguity

Sample *sample_ptr = &sample;

if ((p1 > 0) || sample_ptr->x) { // COMPLIANT: struct member accessors with
Expand Down
Loading