Skip to content

Commit bf45f8c

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 parents 52a4275 + a3abdea commit bf45f8c

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed
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

+8-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
import cpp
2020
import codingstandards.cpp.autosar
2121

22+
class FundamentalType extends BuiltInType {
23+
FundamentalType() { not this instanceof ErroneousType and not this instanceof UnknownType }
24+
}
25+
2226
from Variable v
2327
where
2428
not isExcluded(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
not v.isFromUninstantiatedTemplate(_) and
3034
not (
31-
// find ones where
35+
// Initialized by function call
3236
v.getInitializer().getExpr() instanceof FunctionCall
3337
or
38+
// Initialized by lambda expression
3439
v.getInitializer().getExpr() instanceof LambdaExpression
3540
or
36-
v.getInitializer().getExpr() instanceof ClassAggregateLiteral
41+
// Initialized by non-fundamental type
42+
not v.getInitializer().getExpr().getType() instanceof FundamentalType
3743
) and
3844
// Exclude compiler generated variables
3945
not v.isCompilerGenerated()

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

+1-1
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

+24-3
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+
class Foo {};
110110
void test_loop() {
111-
for (const auto 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> void test_template(std::vector<T> v2) {
130+
for (auto &a : v2) { // COMPLIANT - a is intialized with a function call
131+
a;
132+
}
133+
}
134+
135+
void test_template_instantiation() { test_template<int>({1, 2, 3}); }

0 commit comments

Comments
 (0)