From 296c4e139f5652f7a26f3e179fc2b97223ea695c Mon Sep 17 00:00:00 2001 From: PragmaTwice Date: Sun, 18 Dec 2022 13:14:57 +0800 Subject: [PATCH] Add nodiscard attribute to Status and StatusOr --- CMakeLists.txt | 1 + src/common/status.h | 4 ++-- utils/kvrocks2redis/writer.cc | 12 +++--------- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 406306a1b26..d4a89c26ee4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -177,6 +177,7 @@ add_library(kvrocks_objs OBJECT ${KVROCKS_SRCS}) target_include_directories(kvrocks_objs PUBLIC src src/common ${PROJECT_BINARY_DIR}) target_compile_features(kvrocks_objs PUBLIC cxx_std_17) +# TODO: Add -Werror=unused-result to compile options target_compile_options(kvrocks_objs PUBLIC -Wall -Wpedantic -Wsign-compare -Wreturn-type -fno-omit-frame-pointer) if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") target_compile_options(kvrocks_objs PUBLIC -Wno-pedantic) diff --git a/src/common/status.h b/src/common/status.h index dcd3d9b9782..5c68f60119c 100644 --- a/src/common/status.h +++ b/src/common/status.h @@ -29,7 +29,7 @@ #include #include -class Status { +class [[nodiscard]] Status { public: enum Code : unsigned char { cOK = 0, @@ -121,7 +121,7 @@ template struct IsStatusOr> : std::integral_constant {}; template -struct StatusOr { // NOLINT +struct [[nodiscard]] StatusOr { // NOLINT static_assert(!std::is_same::value, "value_type cannot be Status"); static_assert(!std::is_same::value, "value_type cannot be Status::Code"); static_assert(!IsStatusOr::value, "value_type cannot be StatusOr"); diff --git a/utils/kvrocks2redis/writer.cc b/utils/kvrocks2redis/writer.cc index 44ee4d05da8..423619a0cdb 100644 --- a/utils/kvrocks2redis/writer.cc +++ b/utils/kvrocks2redis/writer.cc @@ -34,22 +34,16 @@ Writer::~Writer() { } Status Writer::Write(const std::string &ns, const std::vector &aofs) { - auto s = GetAofFd(ns); - if (!s.IsOK()) { - return Status(Status::NotOK, s.Msg()); - } + GET_OR_RET(GetAofFd(ns)); for (const auto &aof : aofs) { - Util::Write(aof_fds_[ns], aof); + GET_OR_RET(Util::Write(aof_fds_[ns], aof)); } return Status::OK(); } Status Writer::FlushDB(const std::string &ns) { - auto s = GetAofFd(ns, true); - if (!s.IsOK()) { - return Status(Status::NotOK, s.Msg()); - } + GET_OR_RET(GetAofFd(ns, true)); return Status::OK(); }