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

Commitbf45f8c

Browse files
authored
Merge pull request#412 from github/lcartey/a7-1-5-non-fundamental-types
`A7-1-5`: Exclude initializers of a non-fundamental type
2 parents52a4275 +a3abdea commitbf45f8c

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*`A7-1-5` - exclude auto variables initialized with an expression of non-fundamental type. Typically this occurs when using range based for loops with arrays of non-fundamental types. For example:
2+
```
3+
void iterate(Foo values[]) {
4+
for (auto value : values) { // COMPLIANT (previously false positive)
5+
// ...
6+
}
7+
}
8+
```

‎cpp/autosar/src/rules/A7-1-5/AutoSpecifierNotUsedAppropriatelyInVariableDefinition.ql‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
import cpp
2020
import codingstandards.cpp.autosar
2121

22+
classFundamentalTypeextendsBuiltInType{
23+
FundamentalType(){notthisinstanceofErroneousTypeandnotthisinstanceofUnknownType}
24+
}
25+
2226
fromVariablev
2327
where
2428
notisExcluded(v,
@@ -28,12 +32,14 @@ where
2832
// exclude uninstantiated templates and rely on the instantiated templates, because an uninstantiated template may not contain the information required to determine if the usage is allowed.
2933
notv.isFromUninstantiatedTemplate(_)and
3034
not(
31-
//find ones where
35+
//Initialized by function call
3236
v.getInitializer().getExpr()instanceofFunctionCall
3337
or
38+
// Initialized by lambda expression
3439
v.getInitializer().getExpr()instanceofLambdaExpression
3540
or
36-
v.getInitializer().getExpr()instanceofClassAggregateLiteral
41+
// Initialized by non-fundamental type
42+
notv.getInitializer().getExpr().getType()instanceofFundamentalType
3743
)and
3844
// Exclude compiler generated variables
3945
notv.isCompilerGenerated()

‎cpp/autosar/test/rules/A7-1-5/AutoSpecifierNotUsedAppropriatelyInVariableDefinition.expected‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
| test.cpp:27:8:27:8 | a | Use of auto in variable definition is not the result of a function call, lambda expression, or non-fundamental type initializer. |
55
| test.cpp:28:8:28:8 | b | Use of auto in variable definition is not the result of a function call, lambda expression, or non-fundamental type initializer. |
66
| test.cpp:81:10:81:10 | a | Use of auto in variable definition is not the result of a function call, lambda expression, or non-fundamental type initializer. |
7-
| test.cpp:111:19:111:19 | a | Use of auto in variable definition is not the result of a function call, lambda expression, or non-fundamental type initializer. |
7+
| test.cpp:111:13:111:13 | a | Use of auto in variable definition is not the result of a function call, lambda expression, or non-fundamental type initializer. |

‎cpp/autosar/test/rules/A7-1-5/test.cpp‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,30 @@ void instantiate() {
106106
t381.test_381_1();
107107
t381.test_381_2();
108108
}
109-
109+
classFoo {};
110110
voidtest_loop() {
111-
for (constauto a : {8,9,10}) {
111+
for (auto a : {8,9,10}) {// NON_COMPLIANT - a is initialized with a
112+
// non-constant initializer
113+
a;
114+
}
115+
116+
std::vector<int> v = {1,2,3};
117+
for (auto &a : v) {// COMPLIANT - a is intialized with a function call
118+
a;
119+
}
120+
121+
Foo f1;
122+
Foo f2;
123+
for (auto &a : {f1, f2}) {// COMPLIANT - initialized with a non-fundamental
124+
// type
112125
a;
113126
}
114-
}
127+
}
128+
129+
template<typename T>voidtest_template(std::vector<T> v2) {
130+
for (auto &a : v2) {// COMPLIANT - a is intialized with a function call
131+
a;
132+
}
133+
}
134+
135+
voidtest_template_instantiation() { test_template<int>({1,2,3}); }

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp