Skip to content

Commit 17f3a32

Browse files
committed
M0-1-3: Exclude uninstantiated variable templates
These cause false positives because they are never considered "used".
1 parent 7c7d0ac commit 17f3a32

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

cpp/autosar/test/rules/M0-1-3/UnusedLocalVariable.expected

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
| test.cpp:23:5:23:5 | t | Local variable 't' in 'f1' is not used. |
55
| test.cpp:23:5:23:5 | t | Local variable 't' in 'f1' is not used. |
66
| test.cpp:44:6:44:6 | a | Local variable 'a' in 'test_side_effect_init' is not used. |
7+
| test.cpp:91:5:91:5 | t | Local variable 't' in 'template_function' is not used. |

cpp/autosar/test/rules/M0-1-3/test.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,24 @@ int baz() {
7878
test_constexpr_in_static_assert<int>();
7979
return 0;
8080
}
81+
82+
template <bool... Args> extern constexpr bool all_of_v = true; // COMPLIANT
83+
84+
template <bool B1, bool... Args>
85+
extern constexpr bool all_of_v<B1, Args...> =
86+
B1 && all_of_v<Args...>; // COMPLIANT
87+
88+
void test_template_variable() { all_of_v<true, true, true>; }
89+
90+
template <typename T> void template_function() {
91+
T t; // NON_COMPLIANT - t is never used
92+
T t2; // COMPLIANT - t is used
93+
t2.test(); // Call may not be resolved in uninstantiated template
94+
}
95+
96+
class ClassT {
97+
public:
98+
void test() {}
99+
};
100+
101+
void test_template_function() { template_function<ClassT>(); }

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

+13-2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ class PotentiallyUnusedLocalVariable extends LocalVariable {
4949
// Ignore functions with error expressions as they indicate expressions that the extractor couldn't process
5050
not any(ErrorExpr e).getEnclosingFunction() = f
5151
)
52+
// ) and
53+
// // exclude uninstantiated template members
54+
// not this.isFromUninstantiatedTemplate(_) and
55+
// // Do not report compiler generated variables
56+
// not this.isCompilerGenerated()
5257
}
5358
}
5459

@@ -95,7 +100,9 @@ class PotentiallyUnusedMemberVariable extends MemberVariable {
95100
// Lambda captures are not "real" member variables - it's an implementation detail that they are represented that way
96101
not this = any(LambdaCapture lc).getField() and
97102
// exclude uninstantiated template members
98-
not this.isFromUninstantiatedTemplate(_)
103+
not this.isFromUninstantiatedTemplate(_) and
104+
// Do not report compiler generated variables
105+
not this.isCompilerGenerated()
99106
}
100107
}
101108

@@ -107,7 +114,11 @@ class PotentiallyUnusedGlobalOrNamespaceVariable extends GlobalOrNamespaceVariab
107114
// Not declared in a macro expansion
108115
not isInMacroExpansion() and
109116
// No side-effects from declaration
110-
not declarationHasSideEffects(this)
117+
not declarationHasSideEffects(this) and
118+
// exclude uninstantiated template members
119+
not this.isFromUninstantiatedTemplate(_) and
120+
// Do not report compiler generated variables
121+
not this.isCompilerGenerated()
111122
}
112123
}
113124

0 commit comments

Comments
 (0)