Skip to content

Commit 618dac5

Browse files
committed
A7-1-5: Ignore inits of a non-fundamental type
1 parent c719039 commit 618dac5

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

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+
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/test.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,13 @@ void instantiate() {
108108
}
109109

110110
void test_loop() {
111-
for (const auto a : {8, 9, 10}) {
111+
for (const 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 (const auto a : v) { // COMPLIANT - a is intialized with a function call
112118
a;
113119
}
114120
}

0 commit comments

Comments
 (0)