Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit227d9ed

Browse files
authored
Merge branch 'main' into knewbury01/Declarations8
2 parentsfa6ebd1 +c32a12a commit227d9ed

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
notfinstanceofOperatorand
4142
// Exclude cases where the function call is generated within a macro, as the user of the macro is
42-
// not necessarily able to addressthoes results
43+
// not necessarily able to addressthose results
4344
notfc.isAffectedByMacro()and
44-
// Rule allows disabling this rule where a static_cast<void> is applied
45-
notfc.getExplicitlyConverted().(StaticCast).getActualType()instanceofVoidType
45+
// Rule allows disabling this rule where a static_cast<void> or a C-style cast to void is applied
46+
notexists(Castcast|castinstanceofStaticCastorcastinstanceofCStyleCast|
47+
fc.getExplicitlyConverted()=castand
48+
cast.getActualType()instanceofVoidType
49+
)
4650
selectfc,"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
intf();
24
voidg(int x);
35

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

911
voidtest_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
thisinstanceofAssignOperation
1010
or
11-
// operator op, where op is +=, -=, *=, /=, %=, ^=, &=, |=, >>=
11+
// operator op, where op is +=, -=, *=, /=, %=, ^=, &=, |=, >>=, <<=
1212
exists(stringop|
1313
"operator"+op=this.(FunctionCall).getTarget().getName()and
14-
opin["+=","-=","*=","/=","%=","^=","&=","|=",">>="]
14+
opin["+=","-=","*=","/=","%=","^=","&=","|=",">>=","<<="]
1515
)
1616
}
1717
}
@@ -121,10 +121,10 @@ class UserAssignmentOperator extends AssignmentOperator {
121121
/** An assignment operator of any sort */
122122
classAssignmentOperatorextendsMemberFunction{
123123
AssignmentOperator(){
124-
// operator op, where op is =, +=, -=, *=, /=, %=, ^=, &=, |=, >>=
124+
// operator op, where op is =, +=, -=, *=, /=, %=, ^=, &=, |=, >>=, <<=
125125
exists(stringop|
126126
"operator"+op=this.getName()and
127-
opin["=","+=","-=","*=","/=","%=","^=","&=","|=",">>="]
127+
opin["=","+=","-=","*=","/=","%=","^=","&=","|=",">>=","<<="]
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
QuerygetQuery(){resultinstanceofUnusedParameterSharedQuery}
1313

14+
predicateisMaybeUnusedParameter(Parameterparameter){
15+
parameter.getAnAttribute().toString()="maybe_unused"
16+
}
17+
18+
predicateisLambdaParameter(Parameterparameter){
19+
exists(LambdaExpressionlambda|lambda.getLambdaFunction().getParameter(_)=parameter)
20+
}
21+
1422
querypredicateproblems(UnusedParameterp,stringmessage,Functionf,stringfName){
1523
notisExcluded(p,getQuery())and
24+
notisMaybeUnusedParameter(p)and
25+
(ifisLambdaParameter(p)thenfName="lambda expression"elsefName=f.getQualifiedName())and
1626
f=p.getFunction()and
1727
// Virtual functions are covered by a different rule
1828
notf.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
namespacestd {
22
template<class... Types>classtuple {};
33
template<class... Types> std::tuple<Types...>make_tuple(Types &&...args);
4+
structignore_t {
5+
template<typename T>
6+
constexpr// required since C++14
7+
void
8+
operator=(T &&)constnoexcept {}
9+
};
10+
inlineconst 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
inttest_used(int x) {return x; }// COMPLIANT
57

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

19-
voidtest_no_def(int x);// COMPLIANT - no definition, so cannot be "unused"
21+
voidf(
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+
voidtest_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+
voidtest_no_def(int x);// COMPLIANT - no definition, so cannot be "unused"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp