Skip to content

Commit 119e0d3

Browse files
ckennellycopybara-github
authored andcommitted
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
1 parent 4358cb2 commit 119e0d3

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

absl/base/attributes.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,4 +890,32 @@
890890
#define ABSL_ATTRIBUTE_UNINITIALIZED
891891
#endif
892892

893+
// ABSL_ATTRIBUTE_WARN_UNUSED
894+
//
895+
// Compilers routinely warn about trivial variables that are unused. For
896+
// non-trivial types, this warning is suppressed since the
897+
// constructor/destructor may be intentional and load-bearing, for example, with
898+
// a RAII scoped lock.
899+
//
900+
// For example:
901+
//
902+
// class ABSL_ATTRIBUTE_WARN_UNUSED MyType {
903+
// public:
904+
// MyType();
905+
// ~MyType();
906+
// };
907+
//
908+
// void foo() {
909+
// // Warns with ABSL_ATTRIBUTE_WARN_UNUSED attribute present.
910+
// MyType unused;
911+
// }
912+
//
913+
// See https://clang.llvm.org/docs/AttributeReference.html#warn-unused and
914+
// https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html#index-warn_005funused-type-attribute
915+
#if ABSL_HAVE_CPP_ATTRIBUTE(gnu::warn_unused)
916+
#define ABSL_ATTRIBUTE_WARN_UNUSED [[gnu::warn_unused]]
917+
#else
918+
#define ABSL_ATTRIBUTE_WARN_UNUSED
919+
#endif
920+
893921
#endif // ABSL_BASE_ATTRIBUTES_H_

0 commit comments

Comments
 (0)