Skip to content

Commit 7efe754

Browse files
authored
Merge pull request #753 from github/lcartey/a5-2-6
`A5-2-6`: Ignore cases with the same operator
2 parents 1566129 + 5235b0b commit 7efe754

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- `A5-2-6` - `OperandsOfAlogicalAndOrNotParenthesized.ql`:
2+
- Remove false positives where the operator is identical.
3+
- Improve alert message to clarify which expression needs to be parenthesized.

cpp/autosar/src/rules/A5-2-6/OperandsOfALogicalAndOrNotParenthesized.ql

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,20 @@
1717
import cpp
1818
import codingstandards.cpp.autosar
1919

20-
from BinaryLogicalOperation op, BinaryOperation binop
20+
from BinaryLogicalOperation op, BinaryOperation binop, string leftOrRight
2121
where
2222
not isExcluded(op, OrderOfEvaluationPackage::operandsOfALogicalAndOrNotParenthesizedQuery()) and
23-
op.getAnOperand() = binop and
23+
(
24+
op.getLeftOperand() = binop and
25+
leftOrRight = "Left"
26+
or
27+
op.getRightOperand() = binop and
28+
leftOrRight = "Right"
29+
) and
30+
// Ignore cases with the same operator
31+
not op.getOperator() = binop.getOperator() and
2432
not exists(ParenthesisExpr p | p = binop.getFullyConverted()) and
2533
// Exclude binary operations expanded by a macro.
2634
not binop.isInMacroExpansion()
27-
select op, "Binary $@ operand of logical operation is not parenthesized.", binop, "operator"
35+
select op, "$@ of logical operation " + op.getOperator() + " is not parenthesized.", binop,
36+
leftOrRight + " operand " + binop.getOperator()
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
| test.cpp:3:7:3:23 | ... && ... | Binary $@ operand of logical operation is not parenthesized. | test.cpp:3:7:3:12 | ... > ... | operator |
2-
| test.cpp:3:7:3:23 | ... && ... | Binary $@ operand of logical operation is not parenthesized. | test.cpp:3:17:3:23 | ... < ... | operator |
3-
| test.cpp:7:7:7:24 | ... \|\| ... | Binary $@ operand of logical operation is not parenthesized. | test.cpp:7:19:7:24 | ... > ... | operator |
1+
| test.cpp:3:7:3:23 | ... && ... | $@ of logical operation && is not parenthesized. | test.cpp:3:7:3:12 | ... > ... | Left operand > |
2+
| test.cpp:3:7:3:23 | ... && ... | $@ of logical operation && is not parenthesized. | test.cpp:3:17:3:23 | ... < ... | Right operand < |
3+
| test.cpp:7:7:7:24 | ... \|\| ... | $@ of logical operation \|\| is not parenthesized. | test.cpp:7:19:7:24 | ... > ... | Right operand > |

cpp/autosar/test/rules/A5-2-6/test.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ void f2(int p1, int p2) {
2525
f1();
2626
}
2727

28+
(p1 > 0) && (p2 > 0) && (p1 > p2); // COMPLIANT - no ambiguity
29+
2830
Sample *sample_ptr = &sample;
2931

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

0 commit comments

Comments
 (0)