[clang] [clang][Sema] Check -Wunused-private-field at template instantiation instead of definition (PR #237374)
Nick Desaulniers via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 25 14:11:37 PST 2026
================
@@ -354,3 +354,25 @@ class C {
T t2; // ok
};
}
+
+namespace warn_unused_template {
+struct [[gnu::warn_unused]] WarnUnusedType {};
+
+template <typename T>
----------------
nickdesaulniers wrote:
Hmmm...I think tackling this opened a can of worms. It led to:
1. a revert https://github.com/llvm/llvm-project/pull/243997
2. a growing tail of template-instantiation edge cases https://github.com/llvm/llvm-project/pull/244396#issuecomment-7051561816
As @kazutakahirata [suggests](https://github.com/llvm/llvm-project/pull/244396#issuecomment-7058323100), I think we should split this into smaller and more focused improvements.
Specifically, I think we should fix the false positive in #237370 first (restoring the isDependentContext() / UnusedAttr / isImplicit() checks before WarnUnusedAttr in IsUnusedPrivateField(), like the initial revision of this PR) — at the cost of losing true-positive coverage for unused [[gnu::warn_unused]] private fields inside class templates (Outer2) until we can land smaller, targeted follow-up PRs improving -Wunused-private-field for templates in general.
Two reasons that tradeoff seems worthwhile for now:
- Clang already skips -Wunused-private-field in dependent contexts for all other types (e.g., int) — Outer2 only warned by accident because 5dab5bf0851f checked WarnUnusedAttr before isDependentContext() on the uninstantiated pattern, which is the exact bug causing #237370.
- Even restricting instantiation-time checking (VisitFieldDecl) to just [[gnu::warn_unused]] types runs into the same false positives (if constexpr, friend member/nested templates, explicit member specializations, offsetof, etc.), since libc++ now annotates std::vector/std::string/std::mutex with [[gnu::warn_unused]].
@erichkeane , would you be okay with landing the minimal false-positive fix first so [[gnu::warn_unused]] matches the rest of -Wunused-private-field in dependent contexts, and leaving instantiation-time checking for class templates (Outer2, etc.) to smaller, targeted follow-up PRs?
https://github.com/llvm/llvm-project/pull/237374
More information about the cfe-commits
mailing list