diff options
author | Chris Kennelly <ckennelly@google.com> | 2024-02-12 08:54:50 -0800 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2024-02-12 08:55:50 -0800 |
commit | 119e0d3f74733aff2999d39cb8be99ddcb081c66 (patch) | |
tree | cb8ad0eb3d7a13f70adbbf961617937896bb3de0 | |
parent | 4358cb2f8cb304e64d9a2d2845f472297724e19f (diff) |
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
-rw-r--r-- | absl/base/attributes.h | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/absl/base/attributes.h b/absl/base/attributes.h index 373b6d77..38086a82 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_ |