[clang] [clang] Treat templates referenced in unevaluated operands as used for -Wunused-template (PR #234168)
Younan Zhang via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 23 01:46:56 PDT 2026
================
@@ -908,10 +908,17 @@ static bool ShouldRemoveFromUnused(Sema *SemaRef, const DeclaratorDecl *D) {
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
// If this is a function template and none of its specializations is used,
- // we should warn.
+ // we should warn. A specialization that was implicitly instantiated only
+ // because it was named in an unevaluated operand (decltype, sizeof,
+ // noexcept, a requires-expression) is referenced but never odr-used and
+ // never gets a body; the template still determined the meaning of that
+ // expression, so treat it as a use.
if (FunctionTemplateDecl *Template = FD->getDescribedFunctionTemplate())
for (const auto *Spec : Template->specializations())
- if (ShouldRemoveFromUnused(SemaRef, Spec))
+ if (ShouldRemoveFromUnused(SemaRef, Spec) ||
+ (Spec->getTemplateSpecializationKind() ==
+ TSK_ImplicitInstantiation &&
+ Spec->isReferenced()))
----------------
zyn0217 wrote:
Why aren't those instantiations covered by ShouldRemoveFromUnused? We do have the following things
```cpp
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
// If this is a function template and none of its specializations is used,
// we should warn.
if (FunctionTemplateDecl *Template = FD->getDescribedFunctionTemplate())
for (const auto *Spec : Template->specializations())
if (ShouldRemoveFromUnused(SemaRef, Spec))
return true;
```
https://github.com/llvm/llvm-project/pull/234168
More information about the cfe-commits
mailing list