Skip to content

M0-1-3: Exclude uninstantiated variable templates #620

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions cpp/autosar/test/rules/M0-1-3/UnusedLocalVariable.expected
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
| test.cpp:23:5:23:5 | t | Local variable 't' in 'f1' is not used. |
| test.cpp:23:5:23:5 | t | Local variable 't' in 'f1' is not used. |
| test.cpp:44:6:44:6 | a | Local variable 'a' in 'test_side_effect_init' is not used. |
| test.cpp:91:5:91:5 | t | Local variable 't' in 'template_function' is not used. |
21 changes: 21 additions & 0 deletions cpp/autosar/test/rules/M0-1-3/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,24 @@ int baz() {
test_constexpr_in_static_assert<int>();
return 0;
}

template <bool... Args> extern constexpr bool all_of_v = true; // COMPLIANT

template <bool B1, bool... Args>
extern constexpr bool all_of_v<B1, Args...> =
B1 && all_of_v<Args...>; // COMPLIANT

void test_template_variable() { all_of_v<true, true, true>; }

template <typename T> void template_function() {
T t; // NON_COMPLIANT - t is never used
T t2; // COMPLIANT - t is used
t2.test(); // Call may not be resolved in uninstantiated template
}

class ClassT {
public:
void test() {}
};

void test_template_function() { template_function<ClassT>(); }
15 changes: 13 additions & 2 deletions cpp/common/src/codingstandards/cpp/deadcode/UnusedVariables.qll
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class PotentiallyUnusedLocalVariable extends LocalVariable {
// Ignore functions with error expressions as they indicate expressions that the extractor couldn't process
not any(ErrorExpr e).getEnclosingFunction() = f
)
// ) and
// // exclude uninstantiated template members
// not this.isFromUninstantiatedTemplate(_) and
// // Do not report compiler generated variables
// not this.isCompilerGenerated()
}
}

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

Expand All @@ -107,7 +114,11 @@ class PotentiallyUnusedGlobalOrNamespaceVariable extends GlobalOrNamespaceVariab
// Not declared in a macro expansion
not isInMacroExpansion() and
// No side-effects from declaration
not declarationHasSideEffects(this)
not declarationHasSideEffects(this) and
// exclude uninstantiated template members
not this.isFromUninstantiatedTemplate(_) and
// Do not report compiler generated variables
not this.isCompilerGenerated()
}
}

Expand Down
Loading