Add ABSL_ATTRIBUTE_WARN_UNUSED. This allows us to annotate nontrivial types that should be flagged as unused variables. Compilers otherwise ignore nontrivial variables as their constructors/destructors may be desired and intentional (for example, a scoped lock). PiperOrigin-RevId: 606266618 Change-Id: I64b5f6d32a3cec2f18e0aa9029905f5e836c11a9
diff --git a/absl/base/attributes.h b/absl/base/attributes.h index 373b6d7..38086a8 100644 --- a/absl/base/attributes.h +++ b/absl/base/attributes.h
@@ -890,4 +890,32 @@ #define ABSL_ATTRIBUTE_UNINITIALIZED #endif +// ABSL_ATTRIBUTE_WARN_UNUSED +// +// Compilers routinely warn about trivial variables that are unused. For +// non-trivial types, this warning is suppressed since the +// constructor/destructor may be intentional and load-bearing, for example, with +// a RAII scoped lock. +// +// For example: +// +// class ABSL_ATTRIBUTE_WARN_UNUSED MyType { +// public: +// MyType(); +// ~MyType(); +// }; +// +// void foo() { +// // Warns with ABSL_ATTRIBUTE_WARN_UNUSED attribute present. +// MyType unused; +// } +// +// See https://clang.llvm.org/docs/AttributeReference.html#warn-unused and +// https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html#index-warn_005funused-type-attribute +#if ABSL_HAVE_CPP_ATTRIBUTE(gnu::warn_unused) +#define ABSL_ATTRIBUTE_WARN_UNUSED [[gnu::warn_unused]] +#else +#define ABSL_ATTRIBUTE_WARN_UNUSED +#endif + #endif // ABSL_BASE_ATTRIBUTES_H_