Skip to content

Commit 058ccb4

Browse files
committed
Make query for A0-1-2 more concise
1 parent a1740e7 commit 058ccb4

File tree

1 file changed

+12
-26
lines changed

1 file changed

+12
-26
lines changed

cpp/autosar/src/rules/A0-1-2/UnusedReturnValue.ql

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,14 @@
1616

1717
import cpp
1818
import codingstandards.cpp.autosar
19-
import semmle.code.cpp.dataflow.DataFlow
20-
21-
predicate isStdIgnore(Element element) {
22-
exists(NameQualifier nq |
23-
nq.getQualifiedElement().toString() = "ignore" and
24-
nq.toString() = "std::" and
25-
element.toString() = "ignore"
26-
)
27-
}
28-
29-
/* The statement std::ignore = f() is not recognized an assignment; therefore, we do some painful gymnastics. */
30-
predicate isAssignment(FunctionCall assignment) {
31-
exists(Operator operator |
32-
assignment.getTarget() = operator and
33-
operator.getName() = "operator=" and
34-
// check if this is indeed an operator for assignment by checking if there are no overloads
35-
not exists(operator.getAnOverload())
36-
)
37-
}
38-
39-
predicate isAssignmentOperand(Expr operand) {
40-
exists(FunctionCall assignment | isAssignment(assignment) and operand = assignment.getAChild())
41-
}
19+
import codingstandards.cpp.Operator
20+
import cpp
4221

43-
predicate returnValueIsAssignedToStdIgnore(FunctionCall fc) {
44-
isAssignmentOperand(fc) and exists(Element stdIgnore | isStdIgnore(stdIgnore))
22+
class StdIgnoreVariable extends NamespaceVariable {
23+
StdIgnoreVariable() {
24+
this.hasName("ignore") and
25+
this.getNamespace() instanceof StdNamespace
26+
}
4527
}
4628

4729
/*
@@ -77,5 +59,9 @@ where
7759
cast.getActualType() instanceof VoidType
7860
)
7961
) and
80-
not returnValueIsAssignedToStdIgnore(fc)
62+
// Exclude assignments to std::ignore.
63+
not (
64+
fc.getTarget() instanceof AssignmentOperator and
65+
fc.getAChild().(VariableAccess).getTarget() instanceof StdIgnoreVariable
66+
)
8167
select fc, "Return value from call to $@ is unused.", f, f.getName()

0 commit comments

Comments
 (0)