Skip to content

Commit 09ab74e

Browse files
committed
checkpoint: attempt to catch unused parameters in lambdaexpr
1 parent a1eed75 commit 09ab74e

File tree

2 files changed

+59
-23
lines changed

2 files changed

+59
-23
lines changed

cpp/common/src/codingstandards/cpp/deadcode/UnusedParameters.qll

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,56 @@
11
/**
2-
* A library for identifying parameters which may be unused.
3-
*/
2+
* A library for identifying parameters which may be unused.
3+
*/
44

55
import cpp
66

77
/**
8-
* A `Parameter` which is "usable" within the function.
9-
*
10-
* For this to be the case, the `Function` must have a definition, and that definition must include
11-
* a body block, and the parameter must be a named parameter.
12-
*/
8+
* A `Parameter` which is "usable" within the function.
9+
*
10+
* For this to be the case, the `Function` must have a definition, and that definition must include
11+
* a body block, and the parameter must be a named parameter.
12+
*/
1313
class UsableParameter extends Parameter {
1414
UsableParameter() {
15-
// Find the function associated with the parameter
16-
exists(Function f | this = f.getAParameter() |
17-
// Must have the definition of the function, not just the declaration
18-
f.hasDefinition() and
19-
// There must be a body block associated with the function, otherwise the parameter cannot
20-
// possibly be used
21-
exists(f.getBlock())
22-
) and
15+
(
16+
/* Regular Function */
17+
// Find the function associated with the parameter
18+
exists(Function f | this = f.getAParameter() |
19+
// Must have the definition of the function, not just the declaration
20+
f.hasDefinition() and
21+
// There must be a body block associated with the function, otherwise the parameter cannot
22+
// possibly be used
23+
exists(f.getBlock())
24+
)
25+
or
26+
/* Lambda Expression */
27+
// Find the function associated with the parameter
28+
exists(LambdaExpression lambda, Function f |
29+
this = lambda.getLambdaFunction().getParameter(_)
30+
|
31+
// Must have the definition of the function, not just the declaration
32+
lambda.getLambdaFunction() = f and
33+
f.hasDefinition() and
34+
// There must be a body block associated with the function, otherwise the parameter cannot
35+
// possibly be used
36+
exists(f.getBlock())
37+
)
38+
) and
2339
// Must be a named parameter, because unnamed parameters cannot be referenced
2440
isNamed()
2541
}
2642
}
2743

2844
/**
29-
* A `Parameter` which is usable but not directly used in the local context.
30-
*/
45+
* A `Parameter` which is usable but not directly used in the local context.
46+
*/
3147
class UnusedParameter extends UsableParameter {
3248
UnusedParameter() { not this instanceof UsedParameter }
3349
}
3450

3551
/**
36-
* A `Parameter` which is used in the local context.
37-
*/
52+
* A `Parameter` which is used in the local context.
53+
*/
3854
class UsedParameter extends UsableParameter {
3955
UsedParameter() {
4056
// An access to the parameter exists in the function body
Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
2-
* Provides a library which includes a `problems` predicate for reporting unused parameters.
3-
*/
2+
* Provides a library which includes a `problems` predicate for reporting unused parameters.
3+
*/
44

55
import cpp
66
import codingstandards.cpp.Customizations
@@ -11,11 +11,31 @@ abstract class UnusedParameterSharedQuery extends Query { }
1111

1212
Query getQuery() { result instanceof UnusedParameterSharedQuery }
1313

14+
predicate isMaybeUnusedParameter(Parameter parameter) {
15+
parameter.getAnAttribute().toString() = "maybe_unused"
16+
}
17+
18+
// query
19+
predicate isLambdaParameter(Parameter parameter) {
20+
exists(LambdaExpression lambda | lambda.getLambdaFunction().getParameter(_) = parameter)
21+
}
22+
23+
// query
24+
predicate isLambdaMaybeUnusedParameter(Parameter parameter) {
25+
exists(LambdaExpression lambda | lambda.getLambdaFunction().getParameter(_) = parameter) and
26+
isMaybeUnusedParameter(parameter)
27+
}
28+
29+
query predicate lambdaExprParamHasAccess(Parameter parameter) {
30+
exists(VariableAccess va | isLambdaParameter(parameter) and parameter.getAnAccess() = va)
31+
}
32+
1433
query predicate problems(UnusedParameter p, string message, Function f, string fName) {
1534
not isExcluded(p, getQuery()) and
16-
f = p.getFunction() and
35+
(not isMaybeUnusedParameter(p) and
36+
f = p.getFunction() and
1737
// Virtual functions are covered by a different rule
18-
not f.isVirtual() and
38+
not f.isVirtual()) and
1939
message = "Unused parameter '" + p.getName() + "' for function $@." and
2040
fName = f.getQualifiedName()
2141
}

0 commit comments

Comments
 (0)