[cross-posting this to both the GCC and Clang communities]

I have two specific issues on the subject I would like to discuss below. But first a quick overview:

C++ has two attributes dealing with warnings about unused entities:

* In one direction (making the compiler suppress warnings it would otherwise emit), [[maybe_unused]] (which can be used on lots of different kinds of entities) is meant to suppress warnings about otherwise unused entities.

* In the other direction (making the compiler emit more warnings than it could otherwise infer), [[nodiscard]] (which can be used on certain types, functions, and constructors) is meant to cause warnings about discarded results of function calls or constructor invocations. (For a [[nodiscard]] type, it warns about discarded results of calls to functions returning that type. For a [[nodiscard]] function, it warns about discarded results of calls to that function. For a [[nodiscard]] constructor, it warns about discarded objects initialized via that constructor.)

GCC and (through its GCC compatibility) Clang have three additional non-standard attributes:

* __attribute__((unused)) behaves mostly the same as [[maybe_unused]].

The one difference is that __attribute__((unused)) applied to a type does not warn about that type being unused, but rather warns about unreferenced variables of that type. And it appears that both GCC and Clang implement [[maybe_unused]] with the same semantics as __attribute__((unused)), and cause [[maybe_unused]] applied to a type to warn about unreferenced variables of that type. The mailing list thread starting at <https://lists.isocpp.org/std-discussion/2023/04/2158.php> "[[maybe_unused]] classes" discussed this, and argues that those special semantics of __attribute__((unused)) and [[maybe_unused]] applied to a type are not actually useful: The GCC documentation cites as a use case "lock or thread classes, which are usually defined and then not referenced, but contain constructors and destructors that have nontrivial bookkeeping functions". But the presence of those non-trivial con-/destructors will already prevent the compiler from emitting warnings about seemingly unused variables of such types. So applying __attribute__((unused)) to such types looks like it does not bring any additional value. Or what do other people think?

* __attribute__((warn_unused_result)) (which can only be applied to functions) behaves the same as [[nodiscard]] applied to functions.

* __attribute__((warn_unused)) (which can be applied to class types) is meant to allow warnings about unreferenced variables of the given type, where the compiler could otherwise not infer that those variables are truely unused (because the type has non-trivial con-/destructors). (This attribute does not have a standard counterpart.)

Similarly to how [[nodiscard]] can be applied to individual constructors, it looks beneficial to me to allow __attribute__((warn_unused)) to be applied to individual constructors, too. One example use case is a RAII class that has one constructor that does not acquire a resource (often the default constructor) and another constructor that does acquire a resource. So the class itself cannot be marked __attribute__((warn_unused)). But if the non-acquiring constructor could individually be marked __attribute__((warn_unused)), the compiler could warn about unreferenced variables that are initialized via that constructor. <https://reviews.llvm.org/D148505> "Allow `__attribute__((warn_unused))` on individual constructors" would implement that for Clang---but was met with some reservation for now, due to the already somewhat confusing landscape of standard and GCC/Clang-specific attributes guiding warnings about unused entities as outlined in this post. What do other people think about it? Would it be something that GCC would also want to implement?

Reply via email to