Skip to content

Commit 8979ba0

Browse files
authored
Merge pull request #322 from github/lcartey/fix-range-based-for-loops
`M8-0-1`/`A7-1-5`: Exclude compiler generated DeclStmts
2 parents 52e3a9d + 608fc82 commit 8979ba0

6 files changed

+22
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
* `A7-1-5` - exclude compiler generated variables, such as those generated by for loops.
2+
* `M8-0-1` - exclude compiler generated variables, such as those generated by for loops.

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ where
3434
v.getInitializer().getExpr() instanceof LambdaExpression
3535
or
3636
v.getInitializer().getExpr() instanceof ClassAggregateLiteral
37-
)
37+
) and
38+
// Exclude compiler generated variables
39+
not v.isCompilerGenerated()
3840
select v,
3941
"Use of auto in variable definition is not the result of a function call, lambda expression, or non-fundamental type initializer."

cpp/autosar/src/rules/M8-0-1/MultipleLocalDeclarators.ql

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,7 @@ import codingstandards.cpp.autosar
2020
from DeclStmt ds
2121
where
2222
not isExcluded(ds, InitializationPackage::multipleLocalDeclaratorsQuery()) and
23-
count(ds.getADeclaration()) > 1
23+
count(Declaration d | d = ds.getADeclaration()) > 1 and
24+
// Not a compiler generated `DeclStmt`, such as in the range-based for loop
25+
not ds.isCompilerGenerated()
2426
select ds, "Declaration list contains more than one declaration."

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

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +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. |

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

+6
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,10 @@ void instantiate() {
105105
Test_381<int> t381;
106106
t381.test_381_1();
107107
t381.test_381_2();
108+
}
109+
110+
void test_loop() {
111+
for (const auto a : {8, 9, 10}) {
112+
a;
113+
}
108114
}

cpp/autosar/test/rules/M8-0-1/test.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,10 @@ class ClassA {
1515
int m1, m2; // NON_COMPLIANT
1616
int m3; // COMPLIANT
1717
};
18+
19+
#include <vector>
20+
void test_loop(std::vector<ClassA> v) {
21+
for (const auto b : v) { // COMPLIANT - DeclStmt is compiler generated
22+
b;
23+
}
24+
}

0 commit comments

Comments
 (0)