From 5cf4eb3a535c88c2d9ee34f1ee449ee6ccfaf939 Mon Sep 17 00:00:00 2001 From: "Ralph J. Steinhagen" <46007894+RalphSteinhagen@users.noreply.github.com> Date: Thu, 9 Nov 2023 11:07:16 +0100 Subject: [PATCH] fixes 'std::ptrdiff_t' to 'std::size_t' casting error and suppresses `-Wuseless-cast` warning for gcc and clang (#306) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fixes 'std::ptrdiff_t' to 'std::size_t' casting error addresses this compiler warning: ```text [..]/magic_enum_utility.hpp:101:31: warning: conversion to ‘std::size_t’ {aka ‘long unsigned int’} from ‘std::ptrdiff_t’ {aka ‘long int’} may change the sign of the result [-Wsign-conversion] 101 | return enum_value(index); | ^~~~~ ``` * suppresses `-Wuseless-cast` for static_cast('\0') needed in case 'char_type' is 'char' (common on Linux but rare on Windows?) ```text [..]/magic_enum.hpp:275:114: warning: useless cast to type ‘using magic_enum::char_type = using std::basic_string_view::value_type = char’ {aka ‘char’} [-Wuseless-cast] 275 | constexpr static_str(string_view str, std::integer_sequence) noexcept : chars_{str[I]..., static_cast('\0')} {} | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ ``` --- include/magic_enum.hpp | 2 ++ include/magic_enum_utility.hpp | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/include/magic_enum.hpp b/include/magic_enum.hpp index 53e01c0..79f4455 100644 --- a/include/magic_enum.hpp +++ b/include/magic_enum.hpp @@ -69,9 +69,11 @@ # pragma clang diagnostic push # pragma clang diagnostic ignored "-Wunknown-warning-option" # pragma clang diagnostic ignored "-Wenum-constexpr-conversion" +# pragma clang diagnostic ignored "-Wuseless-cast" // suppresses 'static_cast('\0')' for char_type = char (common on Linux). #elif defined(__GNUC__) # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wmaybe-uninitialized" // May be used uninitialized 'return {};'. +# pragma GCC diagnostic ignored "-Wuseless-cast" // suppresses 'static_cast('\0')' for char_type = char (common on Linux). #elif defined(_MSC_VER) # pragma warning(push) # pragma warning(disable : 26495) // Variable 'static_str::chars_' is uninitialized. diff --git a/include/magic_enum_utility.hpp b/include/magic_enum_utility.hpp index 42f0ec1..20dda97 100644 --- a/include/magic_enum_utility.hpp +++ b/include/magic_enum_utility.hpp @@ -84,7 +84,7 @@ template > if (const auto i = enum_index(value)) { const std::ptrdiff_t index = (static_cast(*i) + n); if (index >= 0 && index < count) { - return enum_value(index); + return enum_value(static_cast(index)); } } return {}; @@ -98,7 +98,7 @@ template > if (const auto i = enum_index(value)) { const std::ptrdiff_t index = ((((static_cast(*i) + n) % count) + count) % count); if (index >= 0 && index < count) { - return enum_value(index); + return enum_value(static_cast(index)); } } return MAGIC_ENUM_ASSERT(false), value; @@ -112,7 +112,7 @@ template > if (const auto i = enum_index(value)) { const std::ptrdiff_t index = (static_cast(*i) - n); if (index >= 0 && index < count) { - return enum_value(index); + return enum_value(static_cast(index)); } } return {}; @@ -126,7 +126,7 @@ template > if (const auto i = enum_index(value)) { const std::ptrdiff_t index = ((((static_cast(*i) - n) % count) + count) % count); if (index >= 0 && index < count) { - return enum_value(index); + return enum_value(static_cast(index)); } } return MAGIC_ENUM_ASSERT(false), value;