https://github.com/AaronBallman created https://github.com/llvm/llvm-project/pull/201881
Basically, this attribute is useful for getting -Wunused-variable diagnostics from class types with a nontrivial constructor or destructor. >From e3eb8e1535a7cdc0679c4f41ddecc2f221e82475 Mon Sep 17 00:00:00 2001 From: Aaron Ballman <[email protected]> Date: Fri, 5 Jun 2026 12:02:03 -0400 Subject: [PATCH] Document the warn_unused attribute Basically, this attribute is useful for getting -Wunused-variable diagnostics from class types with a nontrivial constructor or destructor. --- clang/include/clang/Basic/Attr.td | 2 +- clang/include/clang/Basic/AttrDocs.td | 29 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index 7af72333d651c..15e73f4dd66ad 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -3898,7 +3898,7 @@ def VecReturn : InheritableAttr { def WarnUnused : InheritableAttr { let Spellings = [GCC<"warn_unused">]; let Subjects = SubjectList<[Record]>; - let Documentation = [Undocumented]; + let Documentation = [WarnUnusedDocs]; let SimpleHandler = 1; } diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td index f54f8e8f90596..d4d9cfa5d3e9c 100644 --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -388,6 +388,35 @@ pointer, but not any provenance of the allocation: }]; } +def WarnUnusedDocs : Documentation { + let Category = DocCatType; + let Content = [{ +The ``warn_unused`` attribute can be placed on the declaration of a structure or union type. +When the ``-Wunused-variable`` diagnostic is enabled, local variables of types which have a non-trivial constructor or destructor are considered "used" by virtue of the constructor or destructor invocations involved. +Declaring the type with the ``warn_unused`` attribute strengthens the "use" requirements to require a direct access to the variable, such as passing it as an argument to a call, calling a member function on it, taking the address of it, etc. + +This attribute is available in both C and C++ language modes but is primarily useful in C++ for classes which have a non-trivial constructor or destructor. + +..code-block:: c++ + + struct [[gnu::warn_unused]] S { + S(); + ~S(); + }; + + struct T { + T(); + ~T(); + }; + + void func() { + S s; // -Wunused-variable warning + T t; // No -Wunused-variable warning + } + + }]; +} + def MaybeUndefDocs : Documentation { let Category = DocCatVariable; let Content = [{ _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
