Skip to content

Commit 227d9ed

Browse files
authored
Merge branch 'main' into knewbury01/Declarations8
2 parents fa6ebd1 + c32a12a commit 227d9ed

File tree

8 files changed

+72
-16
lines changed

8 files changed

+72
-16
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
import cpp
1818
import codingstandards.cpp.autosar
19-
import semmle.code.cpp.dataflow.DataFlow
19+
import codingstandards.cpp.Operator
20+
import cpp
2021

2122
/*
2223
* This query performs a simple syntactic check to ensure that the return value of the function is
@@ -39,8 +40,11 @@ where
3940
// so the rule does not require the use of the return value
4041
not f instanceof Operator and
4142
// Exclude cases where the function call is generated within a macro, as the user of the macro is
42-
// not necessarily able to address thoes results
43+
// not necessarily able to address those results
4344
not fc.isAffectedByMacro() and
44-
// Rule allows disabling this rule where a static_cast<void> is applied
45-
not fc.getExplicitlyConverted().(StaticCast).getActualType() instanceof VoidType
45+
// Rule allows disabling this rule where a static_cast<void> or a C-style cast to void is applied
46+
not exists(Cast cast | cast instanceof StaticCast or cast instanceof CStyleCast |
47+
fc.getExplicitlyConverted() = cast and
48+
cast.getActualType() instanceof VoidType
49+
)
4650
select fc, "Return value from call to $@ is unused.", f, f.getName()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| test.cpp:10:3:10:3 | call to f | Return value from call to $@ is unused. | test.cpp:1:5:1:5 | f | f |
1+
| test.cpp:12:3:12:3 | call to f | Return value from call to $@ is unused. | test.cpp:3:5:3:5 | f | f |

cpp/autosar/test/rules/A0-1-2/test.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <tuple>
2+
13
int f();
24
void g(int x);
35

@@ -8,7 +10,8 @@ class A {
810

911
void test_return_val() {
1012
f(); // NON_COMPLIANT - return value never read
11-
static_cast<void>(f()); // COMPLIANT
13+
static_cast<void>(f()); // COMPLIANT - explicitly ignoring the return value by
14+
// static_cast to void.
1215
int x = f(); // COMPLIANT - according to the rule, even though it's not in
1316
// practice used because the unused assignment would be flagged
1417
// by A0-1-1
@@ -17,4 +20,9 @@ void test_return_val() {
1720
A a2;
1821
a1 + a2; // COMPLIANT - `+` is a call to operator+, but is permitted by the
1922
// rule
20-
}
23+
24+
(void)f(); // COMPLIANT - explicitly ignoring the return value by C-style cast
25+
// to void.
26+
std::ignore = f(); // COMPLIANT - explicitly ignoring the return value by
27+
// assigning to std::ignore.
28+
}

cpp/common/src/codingstandards/cpp/Operator.qll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ class AnyAssignOperation extends Expr {
88
AnyAssignOperation() {
99
this instanceof AssignOperation
1010
or
11-
// operator op, where op is +=, -=, *=, /=, %=, ^=, &=, |=, >>=
11+
// operator op, where op is +=, -=, *=, /=, %=, ^=, &=, |=, >>=, <<=
1212
exists(string op |
1313
"operator" + op = this.(FunctionCall).getTarget().getName() and
14-
op in ["+=", "-=", "*=", "/=", "%=", "^=", "&=", "|=", ">>="]
14+
op in ["+=", "-=", "*=", "/=", "%=", "^=", "&=", "|=", ">>=", "<<="]
1515
)
1616
}
1717
}
@@ -121,10 +121,10 @@ class UserAssignmentOperator extends AssignmentOperator {
121121
/** An assignment operator of any sort */
122122
class AssignmentOperator extends MemberFunction {
123123
AssignmentOperator() {
124-
// operator op, where op is =, +=, -=, *=, /=, %=, ^=, &=, |=, >>=
124+
// operator op, where op is =, +=, -=, *=, /=, %=, ^=, &=, |=, >>=, <<=
125125
exists(string op |
126126
"operator" + op = this.getName() and
127-
op in ["=", "+=", "-=", "*=", "/=", "%=", "^=", "&=", "|=", ">>="]
127+
op in ["=", "+=", "-=", "*=", "/=", "%=", "^=", "&=", "|=", ">>=", "<<="]
128128
)
129129
}
130130
}

cpp/common/src/codingstandards/cpp/rules/unusedparameter/UnusedParameter.qll

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,20 @@ 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+
predicate isLambdaParameter(Parameter parameter) {
19+
exists(LambdaExpression lambda | lambda.getLambdaFunction().getParameter(_) = parameter)
20+
}
21+
1422
query predicate problems(UnusedParameter p, string message, Function f, string fName) {
1523
not isExcluded(p, getQuery()) and
24+
not isMaybeUnusedParameter(p) and
25+
(if isLambdaParameter(p) then fName = "lambda expression" else fName = f.getQualifiedName()) and
1626
f = p.getFunction() and
1727
// Virtual functions are covered by a different rule
1828
not f.isVirtual() and
19-
message = "Unused parameter '" + p.getName() + "' for function $@." and
20-
fName = f.getQualifiedName()
29+
message = "Unused parameter '" + p.getName() + "' for function $@."
2130
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
namespace std {
22
template <class... Types> class tuple {};
33
template <class... Types> std::tuple<Types...> make_tuple(Types &&...args);
4+
struct ignore_t {
5+
template <typename T>
6+
constexpr // required since C++14
7+
void
8+
operator=(T &&) const noexcept {}
9+
};
10+
inline const std::ignore_t ignore; // 'const' only until C++17
411
} // namespace std
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
| test.cpp:6:22:6:22 | x | Unused parameter 'x' for function $@. | test.cpp:6:6:6:16 | test_unused | test_unused |
2-
| test.cpp:14:14:14:14 | x | Unused parameter 'x' for function $@. | test.cpp:14:8:14:8 | b | A::b |
1+
| test.cpp:8:22:8:22 | x | Unused parameter 'x' for function $@. | test.cpp:8:6:8:16 | test_unused | test_unused |
2+
| test.cpp:16:14:16:14 | x | Unused parameter 'x' for function $@. | test.cpp:16:8:16:8 | b | A::b |
3+
| test.cpp:35:14:35:14 | y | Unused parameter 'y' for function $@. | test.cpp:34:9:34:9 | operator() | lambda expression |

cpp/common/test/rules/unusedparameter/test.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// NOTICE: SOME OF THE TEST CASES BELOW ARE ALSO INCLUDED IN THE C TEST CASE AND
22
// CHANGES SHOULD BE REFLECTED THERE AS WELL.
33

4+
#include <tuple>
5+
46
int test_used(int x) { return x; } // COMPLIANT
57

68
void test_unused(int x) {} // NON_COMPLIANT
@@ -16,4 +18,29 @@ class A {
1618
virtual void d(int x, int y) {} // virtual, not covered by this rule
1719
};
1820

19-
void test_no_def(int x); // COMPLIANT - no definition, so cannot be "unused"
21+
void f(
22+
int i, // COMPLIANT
23+
int j, // COMPLIANT
24+
int k, // COMPLIANT
25+
[[maybe_unused]] int l // COMPLIANT: explicitly stated as [[maybe_unused]]
26+
) {
27+
static_cast<void>(i); // COMPLIANT: explicitly ignored by static_cast to void
28+
(void)j; // COMPLIANT: explicitly ignored by c-style cast to void
29+
std::ignore = k; // COMPLIANT: explicitly ignored by assignment to std::ignore
30+
}
31+
32+
void test_lambda_expr() {
33+
auto lambda =
34+
[](int x, // COMPLIANT: used
35+
int y, // NON_COMPLIANT: unused without explicit notice
36+
[[maybe_unused]] int z, // COMPLIANT: stdattribute [[maybe_unused]]
37+
int w, // COMPLIANT: static_cast to void
38+
int u, // COMPLIANT: c-style cast to void
39+
int) { // COMPLIANT: unnamed parameter
40+
static_cast<void>(w);
41+
(void)u;
42+
return x;
43+
};
44+
}
45+
46+
void test_no_def(int x); // COMPLIANT - no definition, so cannot be "unused"

0 commit comments

Comments
 (0)