Skip to content

Commit 5e17d21

Browse files
authored
Merge pull request #414 from github/lcartey/a0-1-1-ignore-incomplete-or-compiler-generated-vars
`A0-1-1`: Ignore incomplete or compiler generated variables
2 parents bf45f8c + 8043222 commit 5e17d21

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

change_notes/2023-10-25-a0-1-1.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
* `A0-1-1` - address a number of false positive issues:
2+
* Exclude compiler-generated variables, such as those generated for range-based for loops.
3+
* Exclude variables in uninstantiated templates, for which we have no precise data on uses.
4+
* Deviations should now be applied to the useless assignment instead of the variable itself.

cpp/autosar/src/rules/A0-1-1/UselessAssignment.ql

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ import codingstandards.cpp.deadcode.UselessAssignments
2020

2121
from SsaDefinition ultimateDef, InterestingStackVariable v
2222
where
23-
not isExcluded(v, DeadCodePackage::uselessAssignmentQuery()) and
23+
not isExcluded(ultimateDef, DeadCodePackage::uselessAssignmentQuery()) and
2424
isUselessSsaDefinition(ultimateDef, v)
2525
select ultimateDef, "Definition of $@ is unused.", v, v.getName()

cpp/autosar/test/rules/A0-1-1/test.cpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,15 @@ int test_useless_assignment(int &x, int p) {
112112
return y;
113113
}
114114

115-
int main() { return 0; }
115+
int main() { return 0; }
116+
117+
#include <vector>
118+
template <typename T> void test_range_based_for_loop_template() {
119+
std::vector<A> values_;
120+
for (auto &elem : values_) { // COMPLIANT - should not report either elem or
121+
// the compiler generated (__range)
122+
// variable in the uninstantiated
123+
// template
124+
elem;
125+
}
126+
}

cpp/common/src/codingstandards/cpp/deadcode/UselessAssignments.qll

+5-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ class InterestingStackVariable extends StackVariable {
4343
// A reference parameter can have an affect outside the enclosing function
4444
not mayEscape(this) and
4545
// Not a loop control variable, explicitly excluded
46-
not this instanceof LoopControlVariable
46+
not this instanceof LoopControlVariable and
47+
// Ignore variables in uninstantiated templates
48+
not this.isFromUninstantiatedTemplate(_) and
49+
// Ignore compiler generated variables, such as those generated for range based for loops
50+
not this.isCompilerGenerated()
4751
}
4852
}
4953

0 commit comments

Comments
 (0)