Skip to content
4 changes: 4 additions & 0 deletions change_notes/2023-10-25-a0-1-1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
* `A0-1-1` - address a number of false positive issues:
* Exclude compiler-generated variables, such as those generated for range-based for loops.
* Exclude variables in uninstantiated templates, for which we have no precise data on uses.
* Deviations can now be applied to the useless assignment as well as the variable itself.
2 changes: 1 addition & 1 deletion cpp/autosar/src/rules/A0-1-1/UselessAssignment.ql
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ import codingstandards.cpp.deadcode.UselessAssignments

from SsaDefinition ultimateDef, InterestingStackVariable v
where
not isExcluded(v, DeadCodePackage::uselessAssignmentQuery()) and
not isExcluded(ultimateDef, DeadCodePackage::uselessAssignmentQuery()) and
isUselessSsaDefinition(ultimateDef, v)
select ultimateDef, "Definition of $@ is unused.", v, v.getName()
13 changes: 12 additions & 1 deletion cpp/autosar/test/rules/A0-1-1/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,15 @@ int test_useless_assignment(int &x, int p) {
return y;
}

int main() { return 0; }
int main() { return 0; }

#include <vector>
template <typename T> void test_range_based_for_loop_template() {
std::vector<A> values_;
for (auto &elem : values_) { // COMPLIANT - should not report either elem or
// the compiler generated (__range)
// variable in the uninstantiated
// template
elem;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ class InterestingStackVariable extends StackVariable {
// A reference parameter can have an affect outside the enclosing function
not mayEscape(this) and
// Not a loop control variable, explicitly excluded
not this instanceof LoopControlVariable
not this instanceof LoopControlVariable and
// Ignore variables in uninstantiated templates
not this.isFromUninstantiatedTemplate(_) and
// Ignore compiler generated variables, such as those generated for range based for loops
not this.isCompilerGenerated()
}
}

Expand Down