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

A7-1-5: Exclude initializers of a non-fundamental type#412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
lcartey merged 4 commits intomainfromlcartey/a7-1-5-non-fundamental-types
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletionschange_notes/2023-10-24-a7-1-5-non-fundamental.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
* `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:
```
void iterate(Foo values[]) {
for (auto value : values) { // COMPLIANT (previously false positive)
// ...
}
}
```
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,6 +19,10 @@
import cpp
import codingstandards.cpp.autosar

class FundamentalType extends BuiltInType {
FundamentalType() { not this instanceof ErroneousType and not this instanceof UnknownType }
}

from Variable v
where
not isExcluded(v,
Expand All@@ -28,12 +32,14 @@ where
// 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.
not v.isFromUninstantiatedTemplate(_) and
not (
//find ones where
//Initialized by function call
v.getInitializer().getExpr() instanceof FunctionCall
or
// Initialized by lambda expression
v.getInitializer().getExpr() instanceof LambdaExpression
or
v.getInitializer().getExpr() instanceof ClassAggregateLiteral
// Initialized by non-fundamental type
not v.getInitializer().getExpr().getType() instanceof FundamentalType
) and
// Exclude compiler generated variables
not v.isCompilerGenerated()
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,4 +4,4 @@
| 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. |
| 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. |
| 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. |
| 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. |
| 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. |
27 changes: 24 additions & 3 deletionscpp/autosar/test/rules/A7-1-5/test.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -106,9 +106,30 @@ void instantiate() {
t381.test_381_1();
t381.test_381_2();
}

class Foo {};
void test_loop() {
for (const auto a : {8, 9, 10}) {
for (auto a : {8, 9, 10}) { // NON_COMPLIANT - a is initialized with a
// non-constant initializer
a;
}

std::vector<int> v = {1, 2, 3};
for (auto &a : v) { // COMPLIANT - a is intialized with a function call
a;
}

Foo f1;
Foo f2;
for (auto &a : {f1, f2}) { // COMPLIANT - initialized with a non-fundamental
// type
a;
}
}
}

template <typename T> void test_template(std::vector<T> v2) {
for (auto &a : v2) { // COMPLIANT - a is intialized with a function call
a;
}
}

void test_template_instantiation() { test_template<int>({1, 2, 3}); }

[8]ページ先頭

©2009-2025 Movatter.jp