Skip to content

Commit

Permalink
Add ABSL_ATTRIBUTE_WARN_UNUSED.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ckennelly authored and copybara-github committed Feb 12, 2024
1 parent 4358cb2 commit 119e0d3
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions absl/base/attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_

0 comments on commit 119e0d3

Please sign in to comment.