From 547475295a9bb2aca6f06074caa93a08f6625691 Mon Sep 17 00:00:00 2001 From: michael-grunder Date: Sun, 5 Oct 2025 21:26:26 -0700 Subject: [PATCH] Introduce "must use attribute" Conditionally add `[[nodiscard]]` (c23) or `__attribute__((warn_unused_result))` when the compiler supports it. This commit initially just adds iit to `cluster_map_keyspace` but we can go throughour API adding it where appropriate. --- cluster_library.h | 2 +- common.h | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/cluster_library.h b/cluster_library.h index 731ec03..361a1e5 100644 --- a/cluster_library.h +++ b/cluster_library.h @@ -378,7 +378,7 @@ PHP_REDIS_API redisCluster *cluster_create(double timeout, double read_timeout, int failover, int persistent); PHP_REDIS_API void cluster_free(redisCluster *c, int free_ctx); PHP_REDIS_API void cluster_init_seeds(redisCluster *c, zend_string **seeds, uint32_t nseeds); -PHP_REDIS_API int cluster_map_keyspace(redisCluster *c); +REDIS_NODISCARD PHP_REDIS_API int cluster_map_keyspace(redisCluster *c); PHP_REDIS_API void cluster_free_node(redisClusterNode *node); /* Functions for interacting with cached slots maps */ diff --git a/common.h b/common.h index 559e4a2..8936c8e 100644 --- a/common.h +++ b/common.h @@ -41,6 +41,31 @@ # error "Unknown endianness" #endif +#if defined(_MSC_VER) && !defined(__clang__) +# define REDIS_MSVC 1 +#endif + +#ifndef REDIS_MSVC +# if defined(__has_c_attribute) +# if __has_c_attribute(nodiscard) +# define REDIS_NODISCARD [[nodiscard]] +# endif +# endif +# ifndef REDIS_NODISCARD +# if defined(__has_attribute) +# if __has_attribute(warn_unused_result) +# define REDIS_NODISCARD __attribute__((warn_unused_result)) +# endif +# elif defined(__GNUC__) || defined(__clang__) +# define REDIS_NODISCARD __attribute__((warn_unused_result)) +# else +# define REDIS_NODISCARD +# endif +# endif +#else +# define REDIS_NODISCARD +#endif + #include "backoff.h" typedef enum {