[clang] [Clang] Enable -Wunused-template under -Wall (PR #206123)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 26 09:24:58 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Aditya Medhane (flash1729)
<details>
<summary>Changes</summary>
Uncomment `UnusedTemplate` in the `Unused` diagnostic group so `-Wunused-template` becomes part of `-Wall`.
`-Wunused-template` flags unused function and variable templates with internal linkage. In a header, such a template gives every translation unit its own internal-linkage copy, which is a latent ODR violation (ill-formed, no diagnostic required), so enabling this surfaces a real class of bugs and keeps the pattern from reappearing.
### Depends on
The in-tree occurrences were cleaned up first in a series of NFC PRs tracked by #<!-- -->202945. This change only builds clean once those land, so CI will be red on any area whose cleanup is not yet merged. Remaining prerequisites:
- #<!-- -->202969
- #<!-- -->202971
- #<!-- -->202973
- #<!-- -->202974
- #<!-- -->202988
Closes #<!-- -->202945
cc @<!-- -->AaronBallman @<!-- -->vgvassilev
---
Full diff: https://github.com/llvm/llvm-project/pull/206123.diff
5 Files Affected:
- (modified) clang/docs/ReleaseNotes.rst (+4)
- (modified) clang/include/clang/Basic/DiagnosticGroups.td (+1-1)
- (modified) clang/test/Misc/warning-wall.c (+2)
- (modified) clang/test/SemaCXX/warn-func-not-needed.cpp (+1-1)
- (modified) clang/test/SemaCXX/warn-variable-not-needed.cpp (+1-1)
``````````diff
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index d1303d4c98507..a4c1381836d75 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -676,6 +676,10 @@ Improvements to Clang's diagnostics
- Diagnostics for the C++11 range-based for statement now report the correct
iterator type in notes for invalid iterator types.
+- ``-Wunused-template`` is now part of ``-Wall``. It diagnoses unused function
+ and variable templates with internal linkage, which in a header is a latent
+ ODR hazard. It can be disabled with ``-Wno-unused-template``. (#GH202945)
+
Improvements to Clang's time-trace
----------------------------------
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td
index 6418ab4757a0c..17f4629c1c4c4 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -1359,7 +1359,7 @@ def Conversion
def Unused : DiagGroup<"unused",
[UnusedArgument, UnusedFunction, UnusedLabel,
// UnusedParameter, (matches GCC's behavior)
- // UnusedTemplate, (clean-up libc++ before enabling)
+ UnusedTemplate,
// UnusedMemberFunction, (clean-up llvm before enabling)
UnusedPrivateField, UnusedLambdaCapture,
UnusedLocalTypedef, UnusedValue, UnusedVariable,
diff --git a/clang/test/Misc/warning-wall.c b/clang/test/Misc/warning-wall.c
index 6d6c4e562400f..ade9cbaace77e 100644
--- a/clang/test/Misc/warning-wall.c
+++ b/clang/test/Misc/warning-wall.c
@@ -73,6 +73,8 @@ CHECK-NEXT: -Wunused-argument
CHECK-NEXT: -Wunused-function
CHECK-NEXT: -Wunneeded-internal-declaration
CHECK-NEXT: -Wunused-label
+CHECK-NEXT: -Wunused-template
+CHECK-NEXT: -Wunneeded-internal-declaration
CHECK-NEXT: -Wunused-private-field
CHECK-NEXT: -Wunused-lambda-capture
CHECK-NEXT: -Wunused-local-typedef
diff --git a/clang/test/SemaCXX/warn-func-not-needed.cpp b/clang/test/SemaCXX/warn-func-not-needed.cpp
index cb3cae4cd6c76..74438543c43b7 100644
--- a/clang/test/SemaCXX/warn-func-not-needed.cpp
+++ b/clang/test/SemaCXX/warn-func-not-needed.cpp
@@ -10,7 +10,7 @@ void foo() {
}
namespace test1_template {
-template <typename T> static void f() {}
+template <typename T> static void f() {} // expected-warning {{unused function template}}
template <> void f<int>() {} // expected-warning {{function 'f<int>' is not needed and will not be emitted}}
template <typename T>
void foo() {
diff --git a/clang/test/SemaCXX/warn-variable-not-needed.cpp b/clang/test/SemaCXX/warn-variable-not-needed.cpp
index 272c8998d15c0..d234e9140e3fd 100644
--- a/clang/test/SemaCXX/warn-variable-not-needed.cpp
+++ b/clang/test/SemaCXX/warn-variable-not-needed.cpp
@@ -4,7 +4,7 @@ namespace test1 {
static int abc = 42; // expected-warning {{variable 'abc' is not needed and will not be emitted}}
namespace {
- template <typename T> int abc_template = 0;
+ template <typename T> int abc_template = 0; // expected-warning {{unused variable template}}
template <> int abc_template<int> = 0; // expected-warning {{variable 'abc_template<int>' is not needed and will not be emitted}}
} // namespace
template <typename T>
``````````
</details>
https://github.com/llvm/llvm-project/pull/206123
More information about the cfe-commits
mailing list