This is an automated email from the ASF dual-hosted git repository. alexey pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/kudu.git
commit 8f136bb21c71de13f643a19d9308140a1b76d0d2 Author: Alexey Serbin <[email protected]> AuthorDate: Wed Feb 19 13:47:05 2025 -0800 [util] add 'nodiscard' attribute for Status This patch helps to spot sloppy error handling and catch related mistakes and bugs at compile time once Status class is marked with the 'nodiscard' attribute [1]. It has a potential of saving us time doing debugging and troubleshooting, at least I know a few occasions where it should have helped :) With this patch, a non-handled result Status produces a compiler's warning when compiling with CLANG or GCC13 and newer. People developing on macOS and Ubuntu 24.04 can see that with this patch, and our ASAN and TSAN pre-commit builds should produce corresponding warnings as well. A follow-up changelist [2] turns warnings into errors. Once the sloppiness in error handling is addressed throughout the existing code, [2] should be added to guard against sloppy error handling in newly added code. [1] https://en.cppreference.com/w/cpp/language/attributes/nodiscard [2] https://gerrit.cloudera.org/#/c/22489/ Change-Id: I1ec324e3bb5d10bc35f34ed2614e239a4a150446 Reviewed-on: http://gerrit.cloudera.org:8080/22504 Reviewed-by: Abhishek Chennaka <[email protected]> Tested-by: Alexey Serbin <[email protected]> --- src/kudu/util/status.h | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/kudu/util/status.h b/src/kudu/util/status.h index 5acf59045..107516bc5 100644 --- a/src/kudu/util/status.h +++ b/src/kudu/util/status.h @@ -126,6 +126,24 @@ } \ } while (0) +// Introduce [[nodiscard]] attribute if compiling with the C++17 or newer. +#if __cplusplus >= 201703L + // This works as intended with KUDU_EXPORT and other attributes for LLVM/CLANG + // (at least starting version 11.0.0 that's current version in thirdparty), + // but GCC prior to version 13.0 is touchy if mixing attributes of different + // types/styles, see [1] for details. So, enable this only if compiling with + // CLANG or GCC version 13 and newer. + // + // [1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69585 for details). + #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 13) + #define KUDU_ATTR_NODISCARD [[nodiscard]] // NOLINT(whitespace/braces) + #else + #define KUDU_ATTR_NODISCARD + #endif +#else // #if __cplusplus >= 201703L ... + #define KUDU_ATTR_NODISCARD +#endif // #if __cplusplus >= 201703L ... #else ... + /// @file status.h /// /// This header is used in both the Kudu build as well as in builds of @@ -162,7 +180,7 @@ namespace kudu { /// @brief A representation of an operation's outcome. -class KUDU_EXPORT Status { +class KUDU_EXPORT KUDU_ATTR_NODISCARD Status { public: /// Create an object representing success status. Status() : state_(NULL) { }
